<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 5.4.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"mathieulin.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.12.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"manual","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="这是一个你奶奶都能懂的算法板子">
<meta property="og:type" content="article">
<meta property="og:title" content="算法板子">
<meta property="og:url" content="https://mathieulin.gitee.io/2023/08/29/%E6%9D%BF%E5%AD%90/index.html">
<meta property="og:site_name" content="Mathieu&#39;s blog">
<meta property="og:description" content="这是一个你奶奶都能懂的算法板子">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-08-28T16:00:00.000Z">
<meta property="article:modified_time" content="2023-09-22T13:19:34.227Z">
<meta property="article:author" content="Mathieu Lin">
<meta property="article:tag" content="算法">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://mathieulin.gitee.io/2023/08/29/%E6%9D%BF%E5%AD%90/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://mathieulin.gitee.io/2023/08/29/%E6%9D%BF%E5%AD%90/","path":"2023/08/29/板子/","title":"算法板子"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>算法板子 | Mathieu's blog</title>
  




<link rel="dns-prefetch" href="https://blog-commeny-mh5e4d5jo-mathieulin.vercel.app">
  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="Mathieu's blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Mathieu's blog</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">lzhow的debug路程</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-schedule"><a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%8E%92%E5%BA%8F"><span class="nav-number">1.</span> <span class="nav-text">排序</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F"><span class="nav-number">1.1.</span> <span class="nav-text">快速排序</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%99%AE%E9%80%9A%E5%BF%AB%E6%8E%92"><span class="nav-number">1.1.0.1.</span> <span class="nav-text">普通快排</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93%E5%BF%AB%E6%8E%92"><span class="nav-number">1.1.0.2.</span> <span class="nav-text">结构体快排</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%8C%89%E7%85%A7%E7%BB%93%E6%9E%84%E4%BD%93%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%AD%97%E5%85%B8%E5%BA%8F"><span class="nav-number">1.1.0.3.</span> <span class="nav-text">按照结构体字符串的字典序</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AF%BB%E6%89%BE%E7%AC%ACk%E5%B0%8F%E7%9A%84%E6%95%B0"><span class="nav-number">1.1.0.4.</span> <span class="nav-text">寻找第k小的数</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F"><span class="nav-number">1.2.</span> <span class="nav-text">归并排序</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%BD%92%E5%B9%B6%E6%B1%82%E9%80%86%E5%BA%8F%E5%AF%B9%E6%95%B0"><span class="nav-number">1.2.0.1.</span> <span class="nav-text">归并求逆序对数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%B1%82%E6%9C%80%E8%BF%91%E7%82%B9%E5%AF%B9"><span class="nav-number">1.2.0.2.</span> <span class="nav-text">求最近点对</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A0%86%E6%8E%92%E5%BA%8F%E6%89%A9%E5%B1%95%E2%80%94%E2%80%94%E5%88%86%E6%B2%BB"><span class="nav-number">1.3.</span> <span class="nav-text">堆排序扩展——分治</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9C%80%E5%A4%A7%E5%AD%90%E6%95%B0%E7%BB%84"><span class="nav-number">1.3.0.1.</span> <span class="nav-text">最大子数组</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Strassen%E6%B1%82%E7%9F%A9%E9%98%B5%E4%B9%98%E7%A7%AF"><span class="nav-number">1.3.0.2.</span> <span class="nav-text">Strassen求矩阵乘积</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A0%86%E6%8E%92%E5%BA%8F"><span class="nav-number">1.4.</span> <span class="nav-text">堆排序</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%BB%BA%E7%AB%8B%E6%9C%80%E5%A4%A7%E5%A0%86"><span class="nav-number">1.4.0.1.</span> <span class="nav-text">建立最大堆</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%BB%BA%E7%AB%8B%E6%9C%80%E5%B0%8F%E5%A0%86"><span class="nav-number">1.4.0.2.</span> <span class="nav-text">建立最小堆</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%BB%BA%E7%AB%8B%E6%9C%80%E5%A4%A7%E5%A0%86%E5%B9%B6%E4%BB%8E%E5%A4%A7%E5%88%B0%E5%B0%8F%E6%8E%92%E5%BA%8F"><span class="nav-number">1.4.0.3.</span> <span class="nav-text">建立最大堆并从大到小排序</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%BB%BA%E7%AB%8B%E6%9C%80%E5%A4%A7%E5%A0%86%E5%B9%B6%E8%BF%9B%E8%A1%8C%E4%BB%8E%E5%B0%8F%E5%88%B0%E5%A4%A7%E7%9A%84%E6%8E%92%E5%BA%8F"><span class="nav-number">1.4.0.4.</span> <span class="nav-text">建立最大堆并进行从小到大的排序</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%A9%E7%94%A8%E6%9C%80%E5%B0%8F%E5%A0%86%E5%AF%BB%E6%89%BE%E7%AC%ACk%E5%A4%A7%E7%9A%84%E5%85%83%E7%B4%A0"><span class="nav-number">1.4.0.5.</span> <span class="nav-text">利用最小堆寻找第k大的元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%93%88%E5%A4%AB%E6%9B%BC%E6%A0%91"><span class="nav-number">1.4.0.6.</span> <span class="nav-text">哈夫曼树</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92"><span class="nav-number">2.</span> <span class="nav-text">动态规划</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98"><span class="nav-number">2.1.</span> <span class="nav-text">背包问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#01%E8%83%8C%E5%8C%85"><span class="nav-number">2.1.1.</span> <span class="nav-text">01背包</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85"><span class="nav-number">2.1.2.</span> <span class="nav-text">完全背包</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%80%E5%A4%A7%E4%B8%8A%E5%8D%87%E5%AD%90%E5%BA%8F%E5%88%97"><span class="nav-number">2.2.</span> <span class="nav-text">最大上升子序列</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%92%A2%E7%AE%A1%E5%88%87%E5%89%B2%E9%97%AE%E9%A2%98"><span class="nav-number">2.3.</span> <span class="nav-text">钢管切割问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%81%E6%B0%B4%E7%BA%BF%E8%B0%83%E5%BA%A6"><span class="nav-number">2.4.</span> <span class="nav-text">流水线调度</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%9F%A9%E9%98%B5%E9%93%BE%E4%B9%98%E6%B3%95"><span class="nav-number">2.5.</span> <span class="nav-text">矩阵链乘法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%AD%90%E5%BA%8F%E5%88%97"><span class="nav-number">2.6.</span> <span class="nav-text">最长公共子序列</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%AD%90%E4%B8%B2"><span class="nav-number">2.7.</span> <span class="nav-text">最长公共子串</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%80%E4%BC%98%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91"><span class="nav-number">2.8.</span> <span class="nav-text">最优二叉搜索树</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8A%A0%E5%88%86%E4%BA%8C%E5%8F%89%E6%A0%91"><span class="nav-number">2.9.</span> <span class="nav-text">加分二叉树</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%BE%E8%AE%BA"><span class="nav-number">3.</span> <span class="nav-text">图论</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E5%9B%BE%E7%AE%97%E6%B3%95"><span class="nav-number">3.1.</span> <span class="nav-text">基本图算法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2"><span class="nav-number">3.1.1.</span> <span class="nav-text">广度优先搜索</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%BE%E7%9A%84%E6%B7%B1%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2"><span class="nav-number">3.1.2.</span> <span class="nav-text">图的深度优先搜索</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8B%93%E6%89%91%E6%8E%92%E5%BA%8F"><span class="nav-number">3.1.3.</span> <span class="nav-text">拓扑排序</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91"><span class="nav-number">3.2.</span> <span class="nav-text">最小生成树</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B9%B6%E6%9F%A5%E9%9B%86"><span class="nav-number">3.2.1.</span> <span class="nav-text">并查集</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91-1"><span class="nav-number">3.2.2.</span> <span class="nav-text">最小生成树</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Kruskal%EF%BC%88%E7%A8%80%E7%96%8F%E5%9B%BE-O-ElogV-%EF%BC%89"><span class="nav-number">3.2.2.1.</span> <span class="nav-text">Kruskal（稀疏图$O(ElogV$)）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Prim%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91%EF%BC%88-%E7%A8%A0%E5%AF%86%E5%9B%BEO-V-2-%EF%BC%89"><span class="nav-number">3.2.2.2.</span> <span class="nav-text">Prim最小生成树（$稠密图O(V^2)$）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91%E5%8A%A0%E8%BE%B9%E7%A7%B0%E5%AE%8C%E5%85%A8%E6%A0%91"><span class="nav-number">3.2.2.3.</span> <span class="nav-text">最小生成树加边称完全树</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91%E9%80%86%E5%90%91%E7%94%9F%E6%88%90%E5%AE%8C%E5%85%A8%E5%9B%BE"><span class="nav-number">3.2.3.</span> <span class="nav-text">最小生成树逆向生成完全图</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%80%E7%9F%AD%E8%B7%AF%E5%BE%84"><span class="nav-number">3.3.</span> <span class="nav-text">最短路径</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Bellman-Ford"><span class="nav-number">3.3.0.1.</span> <span class="nav-text">Bellman-Ford</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Dijkstra"><span class="nav-number">3.3.0.2.</span> <span class="nav-text">Dijkstra</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Dijkstra%E9%82%BB%E6%8E%A5%E8%A1%A8%E5%AE%9E%E7%8E%B0"><span class="nav-number">3.3.0.3.</span> <span class="nav-text">Dijkstra邻接表实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Floyd-Warshall"><span class="nav-number">3.3.0.4.</span> <span class="nav-text">Floyd-Warshall</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BD%91%E7%BB%9C%E6%B5%81"><span class="nav-number">3.4.</span> <span class="nav-text">网络流</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Dinic-%E6%9C%80%E5%A4%A7%E6%B5%81"><span class="nav-number">3.4.0.1.</span> <span class="nav-text">Dinic 最大流</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A4%9A%E6%BA%90%E7%82%B9%EF%BC%8C%E5%A4%9A%E6%B1%87%E7%82%B9"><span class="nav-number">3.4.0.2.</span> <span class="nav-text">多源点，多汇点</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A0%91%E7%9A%84%E7%9B%B8%E9%81%87"><span class="nav-number">3.4.1.</span> <span class="nav-text">树的相遇</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B6%E4%BB%96"><span class="nav-number">3.5.</span> <span class="nav-text">其他</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%80%E5%A4%A7%E8%BE%B9%E7%9A%84%E6%9C%80%E5%B0%8F%E5%80%BC"><span class="nav-number">3.5.1.</span> <span class="nav-text">最大边的最小值</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%AE%A1%E7%AE%97%E5%87%A0%E4%BD%95"><span class="nav-number">4.</span> <span class="nav-text">计算几何</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%A4%E6%96%AD%E4%B8%A4%E6%9D%A1%E7%9B%B4%E7%BA%BF%E6%98%AF%E5%90%A6%E7%9B%B8%E4%BA%A4"><span class="nav-number">4.0.1.</span> <span class="nav-text">判断两条直线是否相交</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E8%BE%B9%E5%BD%A2%E9%9D%A2%E7%A7%AF"><span class="nav-number">4.0.2.</span> <span class="nav-text">多边形面积</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#n%E4%B8%AA%E7%82%B9-%E6%B1%82%E5%87%B8%E5%8C%85%E5%91%A8%E9%95%BF"><span class="nav-number">4.0.3.</span> <span class="nav-text">n个点 求凸包周长</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Graham%E5%AF%BB%E6%89%BE%E5%87%B8%E5%8C%85%EF%BC%88%E9%94%99%E8%AF%AF%EF%BC%89"><span class="nav-number">4.0.4.</span> <span class="nav-text">Graham寻找凸包（错误）</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%AD%A3%E7%A1%AE%E7%9A%84"><span class="nav-number">4.0.4.1.</span> <span class="nav-text">正确的</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E8%AE%A1%E7%AE%97%E5%87%B8%E5%8C%85%E9%9D%A2%E7%A7%AF"><span class="nav-number">4.0.4.1.1.</span> <span class="nav-text">计算凸包面积</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%88%A4%E6%96%AD%E7%82%B9%E5%9C%A8%E7%BA%BF%E6%AE%B5%E4%B8%8A"><span class="nav-number">4.0.4.1.2.</span> <span class="nav-text">判断点在线段上</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%88%A4%E6%96%AD%E7%82%B9%E5%9C%A8%E5%87%B8%E5%8C%85%E5%86%85"><span class="nav-number">4.0.4.1.3.</span> <span class="nav-text">判断点在凸包内</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Jarvis%E6%AD%A5%E8%BF%9B%E6%B3%95%E5%AF%BB%E6%89%BE%E5%87%B8%E5%8C%85"><span class="nav-number">4.0.4.2.</span> <span class="nav-text">Jarvis步进法寻找凸包</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%B1%82%E4%B8%A4%E6%9D%A1%E7%BA%BF%E6%AE%B5%E4%BA%A4%E7%82%B9"><span class="nav-number">4.0.4.3.</span> <span class="nav-text">求两条线段交点</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%82%B9%E7%9A%84%E9%87%8D%E5%AE%9A%E4%B9%89"><span class="nav-number">4.0.4.4.</span> <span class="nav-text">点的重定义</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%97%8B%E8%BD%AC%E5%8D%A1%E5%A3%B3"><span class="nav-number">4.0.4.5.</span> <span class="nav-text">旋转卡壳</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%B9%B3%E9%9D%A2%E6%9C%80%E8%BF%9C%E7%82%B9%E5%AF%B9"><span class="nav-number">4.0.4.5.1.</span> <span class="nav-text">平面最远点对</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%B1%82%E8%A7%A3%E5%B9%B3%E9%9D%A2%E7%82%B9%E9%9B%86%E6%9C%80%E5%A4%A7%E4%B8%89%E8%A7%92%E5%BD%A2"><span class="nav-number">4.0.4.5.2.</span> <span class="nav-text">求解平面点集最大三角形</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#FFT"><span class="nav-number">4.1.</span> <span class="nav-text">FFT</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E9%A1%B9%E5%BC%8F%E4%B9%98"><span class="nav-number">4.1.1.</span> <span class="nav-text">多项式乘</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%AB%98%E7%B2%BE%E5%BA%A6%E4%B9%98%E6%B3%95"><span class="nav-number">4.1.2.</span> <span class="nav-text">高精度乘法</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%B6%E4%BB%96-1"><span class="nav-number">5.</span> <span class="nav-text">其他</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C%E5%88%86%E7%AD%94%E6%A1%88%E6%B1%82%E6%9C%80%E5%A4%A7%E5%80%BC%E4%B8%AD%E7%9A%84%E6%9C%80%E5%B0%8F%E5%80%BC"><span class="nav-number">5.1.</span> <span class="nav-text">二分答案求最大值中的最小值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%AC%A7%E6%8B%89%E5%9B%9E%E8%B7%AF"><span class="nav-number">5.2.</span> <span class="nav-text">欧拉回路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#KMP%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%8C%B9%E9%85%8D%E4%BC%98%E5%8C%96"><span class="nav-number">5.3.</span> <span class="nav-text">KMP字符串匹配优化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B1%82%E5%89%8D%E7%BC%80%E7%AD%89%E4%BA%8E%E5%90%8E%E7%BC%80"><span class="nav-number">5.3.1.</span> <span class="nav-text">求前缀等于后缀</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%89%E6%AC%A1%E6%96%B9%E7%A8%8B%E6%B1%82%E6%A0%B9"><span class="nav-number">5.4.</span> <span class="nav-text">三次方程求根</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%87%AA%E7%84%B6%E6%95%B0%E7%9A%84%E6%8B%86%E5%88%86"><span class="nav-number">5.5.</span> <span class="nav-text">自然数的拆分</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8B%93%E6%89%91%E6%8E%92%E5%BA%8F-1"><span class="nav-number">5.6.</span> <span class="nav-text">拓扑排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE"><span class="nav-number">5.7.</span> <span class="nav-text">二分查找</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%B7%A8%E8%B6%8A%E5%80%8D%E6%95%B0%E7%82%B9"><span class="nav-number">5.8.</span> <span class="nav-text">跨越倍数点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8D%95%E8%B0%83%E9%98%9F%E5%88%97%E6%B1%82%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E6%9C%80%E5%A4%A7%E6%9C%80%E5%B0%8F%E5%80%BC"><span class="nav-number">5.9.</span> <span class="nav-text">单调队列求滑动窗口最大最小值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#n%E7%9A%87%E5%90%8E%E9%97%AE%E9%A2%98"><span class="nav-number">5.10.</span> <span class="nav-text">n皇后问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%B4%AA%E5%BF%83%E7%9C%8B%E8%8A%82%E7%9B%AE"><span class="nav-number">5.11.</span> <span class="nav-text">贪心看节目</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%AB%98%E7%B2%BE%E5%BA%A6%E5%8A%A0%E5%87%8F%E6%B3%95"><span class="nav-number">5.12.</span> <span class="nav-text">高精度加减法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%9F%A9%E9%98%B5%E7%9A%84%E8%A1%8C%E5%88%97%E5%BC%8F"><span class="nav-number">5.13.</span> <span class="nav-text">矩阵的行列式</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Mathieu Lin</p>
  <div class="site-description" itemprop="description">这是一个记载了lzhow传奇的一生的博客</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">14</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/mathieulin" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;mathieulin" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:19241032@buaa.edu.cn" title="E-Mail → mailto:19241032@buaa.edu.cn" rel="noopener" target="_blank"><i class="fab fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://121.199.35.110/" title="Todo → http:&#x2F;&#x2F;121.199.35.110&#x2F;" rel="noopener" target="_blank">Todo</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://noietch.cn:88/" title="House → http:&#x2F;&#x2F;noietch.cn:88&#x2F;" rel="noopener" target="_blank">House</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93/0cdfad7c-f758-49ca-becf-6483b8fe95d5.apk" title="Douzero → https:&#x2F;&#x2F;vkceyugu.cdn.bspapp.com&#x2F;VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93&#x2F;0cdfad7c-f758-49ca-becf-6483b8fe95d5.apk" rel="noopener" target="_blank">Douzero</a>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="rss → &#x2F;atom.xml"><i class="fa fa-rss fa-fw"></i>rss</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://mathieulin.github.io/myblog" title="Another-blog → https:&#x2F;&#x2F;mathieulin.github.io&#x2F;myblog" rel="noopener" target="_blank">Another-blog</a>
      </span>
  </div>



        </div>
      </div>
    </div>
    <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1951671851&auto=1&height=66"></iframe>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mathieulin.gitee.io/2023/08/29/%E6%9D%BF%E5%AD%90/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Mathieu Lin">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Mathieu's blog">
      <meta itemprop="description" content="这是一个记载了lzhow传奇的一生的博客">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="算法板子 | Mathieu's blog">
      <meta itemprop="description" content="这是一个你奶奶都能懂的算法板子">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          算法板子
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-08-29 00:00:00" itemprop="dateCreated datePublished" datetime="2023-08-29T00:00:00+08:00">2023-08-29</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-09-22 21:19:34" itemprop="dateModified" datetime="2023-09-22T21:19:34+08:00">2023-09-22</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">算法</span></a>
        </span>
    </span>

  
  
  <span class="post-meta-item">
    
    <span class="post-meta-item-icon">
      <i class="far fa-comment"></i>
    </span>
    <span class="post-meta-item-text">评论数：</span>
  
    <a title="waline" href="/2023/08/29/%E6%9D%BF%E5%AD%90/#waline" itemprop="discussionUrl">
      <span class="post-comments-count waline-comment-count" data-path="/2023/08/29/%E6%9D%BF%E5%AD%90/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
</div>

            <div class="post-description">这是一个你奶奶都能懂的算法板子</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>[ToC]</p>
<h1 id="排序"><a href="#排序" class="headerlink" title="排序"></a>排序</h1><h2 id="快速排序"><a href="#快速排序" class="headerlink" title="快速排序"></a>快速排序</h2><h4 id="普通快排"><a href="#普通快排" class="headerlink" title="普通快排"></a>普通快排</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">cmp</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *a, <span class="type">const</span> <span class="type">void</span>*b)</span>&#123; </span><br><span class="line">    <span class="type">int</span> p = *(<span class="type">int</span>*)a; </span><br><span class="line">    <span class="type">int</span> q = *(<span class="type">int</span>*)b; </span><br><span class="line">    <span class="keyword">return</span> p - q; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="结构体快排"><a href="#结构体快排" class="headerlink" title="结构体快排"></a>结构体快排</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">cmp</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *a, <span class="type">const</span> <span class="type">void</span>*b)</span>&#123; </span><br><span class="line">    StructName p = *(StructName*)a; </span><br><span class="line">    StructName q = *(StructName*)b; </span><br><span class="line">    <span class="keyword">return</span> p.value - q.value; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="按照结构体字符串的字典序"><a href="#按照结构体字符串的字典序" class="headerlink" title="按照结构体字符串的字典序"></a>按照结构体字符串的字典序</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">cmp</span> <span class="params">( <span class="type">const</span> <span class="type">void</span> *a , <span class="type">const</span> <span class="type">void</span> *b )</span> &#123; </span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">strcmp</span>( (*(StructName *)a)-&gt;str , (*(StructName *)b)-&gt;str ); </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="寻找第k小的数"><a href="#寻找第k小的数" class="headerlink" title="寻找第k小的数"></a>寻找第k小的数</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> p,e[<span class="number">101</span>]=&#123;<span class="number">0</span>&#125;;</span><br><span class="line"><span class="type">void</span> <span class="title function_">quicksort</span><span class="params">(<span class="type">int</span> head, <span class="type">int</span> tail)</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> n,i;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数组的数字个数n:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请依次输入n个数字，以空格分开：&quot;</span>);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;e[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入想要得到的从小到大排列的第p个数：&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;p);</span><br><span class="line">	quicksort(<span class="number">1</span>,n);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;该数字是：%d&quot;</span>,e[p]);</span><br><span class="line">	getchar();getchar();</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">quicksort</span><span class="params">(<span class="type">int</span> head,<span class="type">int</span> tail)</span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(head&gt;tail) <span class="keyword">return</span>;</span><br><span class="line">	<span class="type">int</span> i=head,j=tail,temps=e[i];</span><br><span class="line">	<span class="keyword">while</span>(i!=j)&#123;		</span><br><span class="line">		<span class="keyword">while</span>(e[j]&gt;=temps &amp;&amp; i&lt;j)&#123;			</span><br><span class="line">			j--;			</span><br><span class="line">		&#125;		</span><br><span class="line">		<span class="keyword">while</span> (e[i]&lt;=temps &amp;&amp; i&lt;j)&#123;			</span><br><span class="line">			i++;					</span><br><span class="line">		&#125;		</span><br><span class="line">		<span class="keyword">if</span>(i&lt;j)&#123;			</span><br><span class="line">			<span class="type">int</span> m;			</span><br><span class="line">			m=e[i];			</span><br><span class="line">			e[i]=e[j];			</span><br><span class="line">			e[j]=m;			</span><br><span class="line">		&#125;		</span><br><span class="line">	&#125;	</span><br><span class="line">	e[head]=e[i];</span><br><span class="line">	e[i]=temps;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span>(i==p) <span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (i&lt;p) quicksort(i+<span class="number">1</span>,tail);</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (i&gt;p) quicksort(head,i<span class="number">-1</span>);</span><br><span class="line">	<span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="归并排序"><a href="#归并排序" class="headerlink" title="归并排序"></a>归并排序</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">mSort</span><span class="params">(<span class="type">int</span> k[], <span class="type">int</span> tmp[], <span class="type">int</span> left, <span class="type">int</span> right)</span> &#123; </span><br><span class="line">    <span class="type">int</span> center; </span><br><span class="line">    <span class="keyword">if</span>(left &lt; right)&#123; </span><br><span class="line">        center = (left+right)/<span class="number">2</span>; </span><br><span class="line">        mSort(k, tmp, left, center); </span><br><span class="line">        mSort(k, tmp, center+<span class="number">1</span>, right); </span><br><span class="line">        merge(k, tmp, left,center, right); </span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br><span class="line"><span class="type">void</span> <span class="title function_">merge</span><span class="params">(<span class="type">int</span> x[ ],<span class="type">int</span> tmp[ ],<span class="type">int</span> left,<span class="type">int</span> leftend,<span class="type">int</span> rightend)</span>&#123; </span><br><span class="line">    <span class="type">int</span> i=left, j=leftend+<span class="number">1</span>, q=left; </span><br><span class="line">    <span class="keyword">while</span>(i&lt;=leftend &amp;&amp; j&lt;=rightend) &#123; </span><br><span class="line">        <span class="keyword">if</span>(x[i]&lt;=x[j]) tmp[q++]=x[i++]; </span><br><span class="line">        <span class="keyword">else</span> tmp[q++]=x[j++]; </span><br><span class="line">        time++; </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(i&lt;=leftend) tmp[q++]=x[i++]; </span><br><span class="line">    <span class="keyword">while</span>(j&lt;=rightend) tmp[q++]=x[j++]; </span><br><span class="line">    <span class="keyword">for</span>(i=left; i&lt;=rightend; i++) x[i]=tmp[i]; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="归并求逆序对数"><a href="#归并求逆序对数" class="headerlink" title="归并求逆序对数"></a>归并求逆序对数</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> n, a[<span class="number">200005</span>], temp[<span class="number">200005</span>];</span><br><span class="line">ll cnt = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">merge</span><span class="params">(<span class="type">int</span> l, <span class="type">int</span> r, <span class="type">int</span> mid)</span>&#123;</span><br><span class="line">	<span class="type">int</span> i = l, j = mid + <span class="number">1</span>, k = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(i &lt;= mid || j &lt;= r) &#123;</span><br><span class="line">		<span class="keyword">if</span> ((j &gt; r || a[i] &lt;= a[j]) &amp;&amp; i &lt;= mid) temp[k++] = a[i++];</span><br><span class="line">		<span class="keyword">else</span>&#123;</span><br><span class="line">			temp[k++] = a[j++];</span><br><span class="line">			cnt += mid + <span class="number">1</span> - i;</span><br><span class="line">		&#125; </span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;k;i++)&#123;</span><br><span class="line">		a[i+l] = temp[i];</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="type">void</span> <span class="title function_">merge_sort</span><span class="params">(<span class="type">int</span> l, <span class="type">int</span> r)</span> &#123;</span><br><span class="line">	<span class="keyword">if</span> (l &gt;= r) <span class="keyword">return</span> ;</span><br><span class="line">	<span class="type">int</span> mid = (l + r) / <span class="number">2</span>;</span><br><span class="line">	merge_sort(l, mid);</span><br><span class="line">	merge_sort(mid + <span class="number">1</span>, r);</span><br><span class="line">	merge(l,r,mid); </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> n,i;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;a[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	merge_sort(<span class="number">1</span>, n);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%lld\n&quot;</span>, cnt);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="求最近点对"><a href="#求最近点对" class="headerlink" title="求最近点对"></a>求最近点对</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span>&#123;</span></span><br><span class="line">	<span class="type">double</span> x;</span><br><span class="line">	<span class="type">double</span> y;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span> <span class="title">point</span>[100005], <span class="title">temp</span>[100005];</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">cmp</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *a, <span class="type">const</span> <span class="type">void</span> *b)</span>&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Point</span> *<span class="title">p</span> =</span> (<span class="keyword">struct</span> Point *)a;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Point</span> *<span class="title">q</span> =</span> (<span class="keyword">struct</span> Point *)b;</span><br><span class="line">	<span class="keyword">if</span>(<span class="built_in">fabs</span>(p-&gt;x - q-&gt;x)&gt;<span class="number">1e-7</span>)&#123;</span><br><span class="line">		<span class="keyword">if</span>(p-&gt;x - q-&gt;x &gt; <span class="number">1e-7</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="keyword">if</span>(p-&gt;y - q-&gt;y &gt; <span class="number">1e-7</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">cmpy</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *a, <span class="type">const</span> <span class="type">void</span> *b)</span>&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Point</span> *<span class="title">p</span> =</span> (<span class="keyword">struct</span> Point *)a;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Point</span> *<span class="title">q</span> =</span> (<span class="keyword">struct</span> Point *)b;</span><br><span class="line">	<span class="keyword">if</span>(<span class="built_in">fabs</span>(p-&gt;y - q-&gt;y)&gt;<span class="number">1e-7</span>)&#123;</span><br><span class="line">		<span class="keyword">if</span>(p-&gt;y - q-&gt;y &gt; <span class="number">1e-7</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="keyword">if</span>(p-&gt;x - q-&gt;x &gt; <span class="number">1e-7</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">double</span> <span class="title function_">Merge</span><span class="params">(<span class="type">int</span> left,<span class="type">int</span> right,<span class="type">int</span> mid,<span class="type">double</span> Min)</span>&#123;</span><br><span class="line">	<span class="type">double</span> xmin = point[mid].x - Min;</span><br><span class="line">	<span class="type">double</span> xmax = point[mid].x + Min;</span><br><span class="line">	<span class="type">int</span> min=mid,max=mid;</span><br><span class="line">	<span class="keyword">while</span>(point[max].x&lt;=xmax &amp;&amp; max&lt;right) max++;</span><br><span class="line">	<span class="keyword">while</span>(point[min].x&gt;=xmin &amp;&amp; min&gt;left) min--;</span><br><span class="line">	</span><br><span class="line">	<span class="type">int</span> i=<span class="number">0</span>,j;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(j=min;j&lt;=max;j++)&#123;</span><br><span class="line">		temp[i++] = point[j];</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	qsort(temp,i,<span class="keyword">sizeof</span>(temp[<span class="number">0</span>]),cmpy);</span><br><span class="line">	<span class="type">int</span> n=i;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;n<span class="number">-1</span>;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(j=i+<span class="number">1</span>;j&lt;=n<span class="number">-1</span> &amp;&amp; j &lt;= i+<span class="number">8</span> ;j++)&#123;</span><br><span class="line">			<span class="keyword">if</span>((temp[i].x - temp[j].x)*(temp[i].x - temp[j].x) + (temp[i].y - temp[j].y)*(temp[i].y - temp[j].y) &lt; Min)&#123;</span><br><span class="line">				Min = (temp[i].x - temp[j].x)*(temp[i].x - temp[j].x) + (temp[i].y - temp[j].y)*(temp[i].y - temp[j].y);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> Min;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">double</span> <span class="title function_">Min</span><span class="params">(<span class="type">int</span> left,<span class="type">int</span> right)</span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(left==right<span class="number">-1</span>) <span class="keyword">return</span> (point[left].x - point[right].x)*(point[left].x - point[right].x) + (point[left].y - point[right].y)*(point[left].y - point[right].y);</span><br><span class="line">	<span class="type">int</span> mid = (left+right)/<span class="number">2</span>;</span><br><span class="line">	<span class="type">double</span> l1 = Min(left,mid);</span><br><span class="line">	<span class="type">double</span> l2 = Min(mid,right);</span><br><span class="line">	<span class="type">double</span> min;</span><br><span class="line">	<span class="keyword">if</span>(l1&lt;l2) min = l1;</span><br><span class="line">	<span class="keyword">else</span> min = l2;</span><br><span class="line">	</span><br><span class="line">	min = Merge(left,right,mid,min);</span><br><span class="line">	<span class="keyword">return</span> min;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> n;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%lf %lf&quot;</span>,&amp;point[i].x,&amp;point[i].y);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	qsort(point+<span class="number">1</span>,n,<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Point),cmp);</span><br><span class="line">	 </span><br><span class="line">	<span class="type">double</span> min = Min(<span class="number">1</span>,n); </span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%.4lf&quot;</span>,<span class="built_in">pow</span>(min,<span class="number">0.5</span>));</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="堆排序扩展——分治"><a href="#堆排序扩展——分治" class="headerlink" title="堆排序扩展——分治"></a>堆排序扩展——分治</h2><h4 id="最大子数组"><a href="#最大子数组" class="headerlink" title="最大子数组"></a>最大子数组</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"><span class="type">int</span> a[<span class="number">105</span>];</span><br><span class="line"><span class="type">int</span> n;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Return</span> &#123;</span></span><br><span class="line">	<span class="type">int</span> low;</span><br><span class="line">	<span class="type">int</span> high;</span><br><span class="line">	<span class="type">int</span> sum;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> Return <span class="title function_">FindMaxCrossSubArray</span><span class="params">(<span class="type">int</span> low,<span class="type">int</span> mid, <span class="type">int</span> high)</span> &#123;</span><br><span class="line">	<span class="type">int</span> leftSum=<span class="number">-99999</span>, sum=<span class="number">0</span>, rightSum = <span class="number">-99999</span>,leftMax=mid,rightMax=mid;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=mid;i&gt;=low;i--)&#123;</span><br><span class="line">		sum = sum + a[i];</span><br><span class="line">		<span class="keyword">if</span>(sum&gt;leftSum)&#123;</span><br><span class="line">			leftSum = sum;</span><br><span class="line">			leftMax = i;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	sum=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span>(i=mid+<span class="number">1</span>;i&lt;=high;i++)&#123;</span><br><span class="line">		sum = sum + a[i];</span><br><span class="line">		<span class="keyword">if</span>(sum&gt;rightSum)&#123;</span><br><span class="line">			rightSum = sum;</span><br><span class="line">			rightMax = i;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Return</span> <span class="title">A</span>;</span></span><br><span class="line">	A.high = rightMax;</span><br><span class="line">	A.low = leftMax;</span><br><span class="line">	A.sum = rightSum+leftSum;</span><br><span class="line">	<span class="keyword">return</span> A;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> Return <span class="title function_">FindMaxSubArray</span><span class="params">(<span class="type">int</span> low, <span class="type">int</span> high)</span>&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Return</span> <span class="title">A</span>;</span></span><br><span class="line">	<span class="keyword">if</span>(low == high)&#123;</span><br><span class="line">		A.high = high;</span><br><span class="line">		A.low = low;</span><br><span class="line">		A.sum = a[high];</span><br><span class="line">		<span class="keyword">return</span> A;</span><br><span class="line">	&#125; </span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="type">int</span> mid = (high+low)/<span class="number">2</span>;</span><br><span class="line">		<span class="class"><span class="keyword">struct</span> <span class="title">Return</span> <span class="title">A</span>,<span class="title">B</span>,<span class="title">C</span>;</span></span><br><span class="line">		A = FindMaxSubArray(low,mid);</span><br><span class="line">		B = FindMaxSubArray(mid+<span class="number">1</span>,high);</span><br><span class="line">		C = FindMaxCrossSubArray(low,mid,high);</span><br><span class="line">		<span class="keyword">if</span>(A.sum &gt;= B.sum &amp;&amp; A.sum&gt;=C.sum)&#123;</span><br><span class="line">			<span class="keyword">return</span> A;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span>(B.sum&gt;=A.sum &amp;&amp;B.sum&gt;=C.sum)&#123;</span><br><span class="line">			<span class="keyword">return</span> B;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">return</span> C;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;	</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Return</span> <span class="title">A</span>;</span></span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;a[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	A = FindMaxSubArray(<span class="number">1</span>,n);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;最大子数组为%d-%d，和为%d&quot;</span>,A.low,A.high,A.sum);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="Strassen求矩阵乘积"><a href="#Strassen求矩阵乘积" class="headerlink" title="Strassen求矩阵乘积"></a>Strassen求矩阵乘积</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> A[<span class="number">50</span>][<span class="number">50</span>],B[<span class="number">50</span>][<span class="number">50</span>],C[<span class="number">50</span>][<span class="number">50</span>]; </span><br><span class="line"><span class="type">int</span> S1[<span class="number">50</span>][<span class="number">50</span>],S2[<span class="number">50</span>][<span class="number">50</span>],S3[<span class="number">50</span>][<span class="number">50</span>],S4[<span class="number">50</span>][<span class="number">50</span>],S5[<span class="number">50</span>][<span class="number">50</span>],S6[<span class="number">50</span>][<span class="number">50</span>],S7[<span class="number">50</span>][<span class="number">50</span>],S8[<span class="number">50</span>][<span class="number">50</span>],S9[<span class="number">50</span>][<span class="number">50</span>],S10[<span class="number">50</span>][<span class="number">50</span>];</span><br><span class="line"></span><br><span class="line">Strassen()&#123;</span><br><span class="line">	<span class="keyword">if</span>(n==<span class="number">1</span>) <span class="keyword">return</span> C[<span class="number">1</span>][<span class="number">1</span>];</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		Strassen();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> n;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="type">int</span> i,j;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">			<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;a[i][j]);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">			<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;b[i][j]);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n/<span class="number">2</span>;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=n/<span class="number">2</span>;j++)&#123;</span><br><span class="line">			S1[i][j] = B[i][j+n/<span class="number">2</span>]-B[i+n/<span class="number">2</span>][j+n/<span class="number">2</span>];</span><br><span class="line">			S2[i][j] = A[i][j]+A[i][j+n/<span class="number">2</span>];</span><br><span class="line">			S3[i][j] = A[i+n/<span class="number">2</span>][j+n/<span class="number">2</span>]+A[i+n/<span class="number">2</span>][j+n/<span class="number">2</span>];</span><br><span class="line">			S4[i][j] = B[i+n/<span class="number">2</span>][j]-B[i][j];</span><br><span class="line">			S5[i][j] = A[i][j]+A[i+n/<span class="number">2</span>][j+n/<span class="number">2</span>];</span><br><span class="line">			S6[i][j] = B[i][j]+B[i+n/<span class="number">2</span>][j+n/<span class="number">2</span>];</span><br><span class="line">			S7[i][j] = A[i][j+n/<span class="number">2</span>]-A[i+n/<span class="number">2</span>][j+n/<span class="number">2</span>];</span><br><span class="line">			S8[i][j] = B[i+n/<span class="number">2</span>][j+n/<span class="number">2</span>]+B[i+n/<span class="number">2</span>][j+n/<span class="number">2</span>];</span><br><span class="line">			S9[i][j] = A[i][j]-A[i+n/<span class="number">2</span>][j];</span><br><span class="line">			S10[i][j] = B[i][j]+B[i][j+n/<span class="number">2</span>];</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	Strassen();</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="堆排序"><a href="#堆排序" class="headerlink" title="堆排序"></a>堆排序</h2><h4 id="建立最大堆"><a href="#建立最大堆" class="headerlink" title="建立最大堆"></a>建立最大堆</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> a[<span class="number">105</span>] ;</span><br><span class="line"><span class="type">int</span> n;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">MAX_HEAPIFY</span><span class="params">(<span class="type">int</span> i)</span> &#123;</span><br><span class="line">	<span class="type">int</span> l = <span class="number">2</span>*i, r = <span class="number">2</span>*i+<span class="number">1</span>, max, flag;</span><br><span class="line">	<span class="keyword">if</span>(l &lt;= n &amp;&amp; a[l] &gt; a[i])&#123;</span><br><span class="line">		flag = l;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> flag = i;</span><br><span class="line">	<span class="keyword">if</span>(r &lt;= n &amp;&amp; a[r] &gt; a[flag])&#123;</span><br><span class="line">		flag = r;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(flag != i) &#123;</span><br><span class="line">		<span class="type">int</span> temp = a[flag];</span><br><span class="line">		a[flag] = a[i];</span><br><span class="line">		a[i] = temp;</span><br><span class="line">		MAX_HEAPIFY(flag);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">BUILD_MAX_HEAP</span><span class="params">()</span> &#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=n/<span class="number">2</span>;i&gt;=<span class="number">1</span>;i--)&#123;</span><br><span class="line">		MAX_HEAPIFY(i);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;a[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	BUILD_MAX_HEAP();</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>,a[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/*MAX_HEAPIFY(2);  //传入的参数为本来已经为最大堆了，但是改变了的那个数的下标 </span></span><br><span class="line"><span class="comment">	</span></span><br><span class="line"><span class="comment">	for(i=1;i&lt;=n;i++)&#123;</span></span><br><span class="line"><span class="comment">		printf(&quot;%d &quot;,a[i]);</span></span><br><span class="line"><span class="comment">	&#125;</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="建立最小堆"><a href="#建立最小堆" class="headerlink" title="建立最小堆"></a>建立最小堆</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 200005 </span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> a[N] ;</span><br><span class="line"><span class="type">int</span> n;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">MIN_HEAPIFY</span><span class="params">(<span class="type">int</span> i)</span> &#123;</span><br><span class="line">	<span class="type">int</span> l = <span class="number">2</span>*i, r = <span class="number">2</span>*i+<span class="number">1</span>, min, flag;</span><br><span class="line">	<span class="keyword">if</span>(l &lt;= n &amp;&amp; a[l] &lt; a[i])&#123;</span><br><span class="line">		flag = l;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> flag = i;</span><br><span class="line">	<span class="keyword">if</span>(r &lt;= n &amp;&amp; a[r] &lt; a[flag])&#123;</span><br><span class="line">		flag = r;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(flag != i) &#123;</span><br><span class="line">		<span class="type">int</span> temp = a[flag];</span><br><span class="line">		a[flag] = a[i];</span><br><span class="line">		a[i] = temp;</span><br><span class="line">		MIN_HEAPIFY(flag);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">BUILD_MIN_HEAP</span><span class="params">()</span> &#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=n/<span class="number">2</span>;i&gt;=<span class="number">1</span>;i--)&#123;</span><br><span class="line">		MIN_HEAPIFY(i);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;a[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	BUILD_MIN_HEAP();</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>,a[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/*MAX_HEAPIFY(2);  //传入的参数为本来已经为最大堆了，但是改变了的那个数的下标 </span></span><br><span class="line"><span class="comment">	</span></span><br><span class="line"><span class="comment">	for(i=1;i&lt;=n;i++)&#123;</span></span><br><span class="line"><span class="comment">		printf(&quot;%d &quot;,a[i]);</span></span><br><span class="line"><span class="comment">	&#125;</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="建立最大堆并从大到小排序"><a href="#建立最大堆并从大到小排序" class="headerlink" title="建立最大堆并从大到小排序"></a>建立最大堆并从大到小排序</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> h[<span class="number">101</span>],n;</span><br><span class="line"><span class="comment">//交换两个元素 </span></span><br><span class="line"><span class="type">void</span> <span class="title function_">swap</span><span class="params">(<span class="type">int</span> x,<span class="type">int</span> y)</span>&#123;</span><br><span class="line">	<span class="type">int</span> a;</span><br><span class="line">	a=h[x];</span><br><span class="line">	h[x]=h[y];</span><br><span class="line">	h[y]=a;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//向下调整元素</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">siftdown</span><span class="params">(<span class="type">int</span> i)</span>&#123;</span><br><span class="line">	<span class="type">int</span> t,flag=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(<span class="number">2</span>*i&lt;=n&amp;&amp;flag==<span class="number">0</span>)&#123;</span><br><span class="line">		<span class="keyword">if</span>(h[i]&lt;h[<span class="number">2</span>*i]) t=<span class="number">2</span>*i;</span><br><span class="line">		<span class="keyword">else</span> t=i;<span class="comment">//记录下较大的数（父亲和左儿子）的下标 </span></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span> (<span class="number">2</span>*i+<span class="number">1</span>&lt;=n)&#123;</span><br><span class="line">			<span class="keyword">if</span>(h[t]&lt;h[<span class="number">2</span>*i+<span class="number">1</span>]) t=<span class="number">2</span>*i+<span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(t!=i)&#123;</span><br><span class="line">			swap(t,i);</span><br><span class="line">			i=t;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> flag=<span class="number">1</span>;<span class="comment">//表示儿子点超过父亲，循环终止 </span></span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//建立堆的函数(最大堆)</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">creat</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=n/<span class="number">2</span>;i&gt;=<span class="number">1</span>;i--)&#123;</span><br><span class="line">		siftdown(i);</span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//删除最小元素</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">deletemax</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> t;</span><br><span class="line">	t=h[<span class="number">1</span>];</span><br><span class="line">	h[<span class="number">1</span>]=h[n];</span><br><span class="line">	n--;</span><br><span class="line">	siftdown(<span class="number">1</span>);</span><br><span class="line">	<span class="keyword">return</span> t;</span><br><span class="line">&#125; </span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="type">int</span> i,num;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;num);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=num;i++) <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;h[i]);</span><br><span class="line">	n=num;</span><br><span class="line">	</span><br><span class="line">	creat();</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=num;i&gt;=<span class="number">1</span>;i--)&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>,deletemax());</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	getchar();getchar();</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="建立最大堆并进行从小到大的排序"><a href="#建立最大堆并进行从小到大的排序" class="headerlink" title="建立最大堆并进行从小到大的排序"></a>建立最大堆并进行从小到大的排序</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> h[<span class="number">101</span>],n;</span><br><span class="line"><span class="comment">//交换两个元素 </span></span><br><span class="line"><span class="type">void</span> <span class="title function_">swap</span><span class="params">(<span class="type">int</span> x,<span class="type">int</span> y)</span>&#123;</span><br><span class="line">	<span class="type">int</span> a;</span><br><span class="line">	a=h[x];</span><br><span class="line">	h[x]=h[y];</span><br><span class="line">	h[y]=a;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//向下调整元素</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">siftdown</span><span class="params">(<span class="type">int</span> i)</span>&#123;</span><br><span class="line">	<span class="type">int</span> t,flag=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(<span class="number">2</span>*i&lt;=n&amp;&amp;flag==<span class="number">0</span>)&#123;</span><br><span class="line">		<span class="keyword">if</span>(h[i]&lt;h[<span class="number">2</span>*i]) t=<span class="number">2</span>*i;</span><br><span class="line">		<span class="keyword">else</span> t=i;<span class="comment">//记录下较大的数（父亲和左儿子）的下标 </span></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span> (<span class="number">2</span>*i+<span class="number">1</span>&lt;=n)&#123;</span><br><span class="line">			<span class="keyword">if</span>(h[t]&lt;h[<span class="number">2</span>*i+<span class="number">1</span>]) t=<span class="number">2</span>*i+<span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(t!=i)&#123;</span><br><span class="line">			swap(t,i);</span><br><span class="line">			i=t;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> flag=<span class="number">1</span>;<span class="comment">//表示儿子点超过父亲，循环终止 </span></span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//建立堆的函数(最大堆)</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">creat</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=n/<span class="number">2</span>;i&gt;=<span class="number">1</span>;i--)&#123;</span><br><span class="line">		siftdown(i);</span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//将最大元素置于最末 </span></span><br><span class="line"><span class="type">void</span> <span class="title function_">heapsort</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="keyword">while</span> (n&gt;<span class="number">1</span>)&#123;</span><br><span class="line">		swap(<span class="number">1</span>,n);</span><br><span class="line">		n--;</span><br><span class="line">		siftdown(<span class="number">1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="type">int</span> i,num;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;num);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=num;i++) <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;h[i]);</span><br><span class="line">	n=num;</span><br><span class="line">	</span><br><span class="line">	creat();</span><br><span class="line">	heapsort();</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=num;i++)&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>,h[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	getchar();getchar();</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="利用最小堆寻找第k大的元素"><a href="#利用最小堆寻找第k大的元素" class="headerlink" title="利用最小堆寻找第k大的元素"></a>利用最小堆寻找第k大的元素</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> h[<span class="number">101</span>],n,k,a[<span class="number">101</span>];</span><br><span class="line"><span class="comment">//交换两个元素 </span></span><br><span class="line"><span class="type">void</span> <span class="title function_">swap</span><span class="params">(<span class="type">int</span> x,<span class="type">int</span> y)</span>&#123;</span><br><span class="line">	<span class="type">int</span> a;</span><br><span class="line">	a=h[x];</span><br><span class="line">	h[x]=h[y];</span><br><span class="line">	h[y]=a;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//向下调整元素</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">siftdown</span><span class="params">(<span class="type">int</span> i)</span>&#123;</span><br><span class="line">	<span class="type">int</span> t,flag=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(<span class="number">2</span>*i&lt;=k&amp;&amp;flag==<span class="number">0</span>)&#123;</span><br><span class="line">		<span class="keyword">if</span>(h[i]&gt;h[<span class="number">2</span>*i]) t=<span class="number">2</span>*i;</span><br><span class="line">		<span class="keyword">else</span> t=i;<span class="comment">//记录下较小的数（父亲和左儿子）的下标 </span></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span> (<span class="number">2</span>*i+<span class="number">1</span>&lt;=k)&#123;</span><br><span class="line">			<span class="keyword">if</span>(h[t]&gt;h[<span class="number">2</span>*i+<span class="number">1</span>]) t=<span class="number">2</span>*i+<span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(t!=i)&#123;</span><br><span class="line">			swap(t,i);</span><br><span class="line">			i=t;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> flag=<span class="number">1</span>;<span class="comment">//表示儿子点超过父亲，循环终止 </span></span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//建立堆的函数(最小堆)</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">creat</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=k/<span class="number">2</span>;i&gt;=<span class="number">1</span>;i--)&#123;</span><br><span class="line">		siftdown(i);</span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;k);</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;a[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=k;i++) h[i]=a[i];</span><br><span class="line">	</span><br><span class="line">	creat();</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=k+<span class="number">1</span>;i&lt;=n;i++)	&#123;</span><br><span class="line">		<span class="keyword">if</span> (a[i]&gt;h[<span class="number">1</span>])&#123;</span><br><span class="line">			h[<span class="number">1</span>]=a[i];</span><br><span class="line">			siftdown(<span class="number">1</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,h[<span class="number">1</span>]);</span><br><span class="line">	getchar();getchar();</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="哈夫曼树"><a href="#哈夫曼树" class="headerlink" title="哈夫曼树"></a>哈夫曼树</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span> &#123;</span></span><br><span class="line">    <span class="type">int</span> value;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Node</span> *<span class="title">left</span>, *<span class="title">right</span>;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">MinHeap</span> &#123;</span></span><br><span class="line">    <span class="type">unsigned</span> size;</span><br><span class="line">    <span class="type">unsigned</span> capacity;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Node</span> **<span class="title">array</span>;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> Node* <span class="title function_">newNode</span><span class="params">(<span class="type">int</span> value)</span> &#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Node</span>* <span class="title">temp</span> =</span> (<span class="keyword">struct</span> Node*) <span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Node));</span><br><span class="line">    temp-&gt;left = temp-&gt;right = <span class="literal">NULL</span>;</span><br><span class="line">    temp-&gt;value = value;</span><br><span class="line">    <span class="keyword">return</span> temp;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> MinHeap* <span class="title function_">createMinHeap</span><span class="params">(<span class="type">unsigned</span> capacity)</span> &#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">MinHeap</span>* <span class="title">minHeap</span> =</span> (<span class="keyword">struct</span> MinHeap*) <span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> MinHeap));</span><br><span class="line">    minHeap-&gt;size = <span class="number">0</span>;</span><br><span class="line">    minHeap-&gt;capacity = capacity;</span><br><span class="line">    minHeap-&gt;<span class="built_in">array</span> = (<span class="keyword">struct</span> Node**)<span class="built_in">malloc</span>(minHeap-&gt;capacity * <span class="keyword">sizeof</span>(<span class="keyword">struct</span> Node*));</span><br><span class="line">    <span class="keyword">return</span> minHeap;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">swapMinHeapNode</span><span class="params">(<span class="keyword">struct</span> Node** a, <span class="keyword">struct</span> Node** b)</span> &#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Node</span>* <span class="title">t</span> =</span> *a;</span><br><span class="line">    *a = *b;</span><br><span class="line">    *b = t;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">minHeapify</span><span class="params">(<span class="keyword">struct</span> MinHeap* minHeap, <span class="type">int</span> idx)</span> &#123;</span><br><span class="line">    <span class="type">int</span> smallest = idx;</span><br><span class="line">    <span class="type">int</span> left = <span class="number">2</span> * idx + <span class="number">1</span>;</span><br><span class="line">    <span class="type">int</span> right = <span class="number">2</span> * idx + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (left &lt; minHeap-&gt;size &amp;&amp; minHeap-&gt;<span class="built_in">array</span>[left]-&gt;value &lt; minHeap-&gt;<span class="built_in">array</span>[smallest]-&gt;value)</span><br><span class="line">        smallest = left;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (right &lt; minHeap-&gt;size &amp;&amp; minHeap-&gt;<span class="built_in">array</span>[right]-&gt;value &lt; minHeap-&gt;<span class="built_in">array</span>[smallest]-&gt;value)</span><br><span class="line">        smallest = right;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (smallest != idx) &#123;</span><br><span class="line">        swapMinHeapNode(&amp;minHeap-&gt;<span class="built_in">array</span>[smallest], &amp;minHeap-&gt;<span class="built_in">array</span>[idx]);</span><br><span class="line">        minHeapify(minHeap, smallest);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">isSizeOne</span><span class="params">(<span class="keyword">struct</span> MinHeap* minHeap)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (minHeap-&gt;size == <span class="number">1</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> Node* <span class="title function_">extractMin</span><span class="params">(<span class="keyword">struct</span> MinHeap* minHeap)</span> &#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Node</span>* <span class="title">temp</span> =</span> minHeap-&gt;<span class="built_in">array</span>[<span class="number">0</span>];</span><br><span class="line">    minHeap-&gt;<span class="built_in">array</span>[<span class="number">0</span>] = minHeap-&gt;<span class="built_in">array</span>[minHeap-&gt;size - <span class="number">1</span>];</span><br><span class="line">    --minHeap-&gt;size;</span><br><span class="line">    minHeapify(minHeap, <span class="number">0</span>);</span><br><span class="line">    <span class="keyword">return</span> temp;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">insertMinHeap</span><span class="params">(<span class="keyword">struct</span> MinHeap* minHeap, <span class="keyword">struct</span> Node* node)</span> &#123;</span><br><span class="line">    ++minHeap-&gt;size;</span><br><span class="line">    <span class="type">int</span> i = minHeap-&gt;size - <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">while</span> (i &amp;&amp; node-&gt;value &lt; minHeap-&gt;<span class="built_in">array</span>[(i - <span class="number">1</span>)/<span class="number">2</span>]-&gt;value) &#123;</span><br><span class="line">        minHeap-&gt;<span class="built_in">array</span>[i] = minHeap-&gt;<span class="built_in">array</span>[(i - <span class="number">1</span>)/<span class="number">2</span>];</span><br><span class="line">        i = (i - <span class="number">1</span>)/<span class="number">2</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    minHeap-&gt;<span class="built_in">array</span>[i] = node;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">buildMinHeap</span><span class="params">(<span class="keyword">struct</span> MinHeap* minHeap)</span> &#123;</span><br><span class="line">    <span class="type">int</span> n = minHeap-&gt;size - <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = (n - <span class="number">1</span>) / <span class="number">2</span>; i &gt;= <span class="number">0</span>; --i) &#123;</span><br><span class="line">        minHeapify(minHeap, i);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> MinHeap* <span class="title function_">createAndBuildMinHeap</span><span class="params">(<span class="type">int</span> values[], <span class="type">int</span> size)</span> &#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">MinHeap</span>* <span class="title">minHeap</span> =</span> createMinHeap(size);</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; size; ++i) &#123;</span><br><span class="line">        minHeap-&gt;<span class="built_in">array</span>[i] = newNode(values[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    minHeap-&gt;size = size;</span><br><span class="line">    buildMinHeap(minHeap);</span><br><span class="line">    <span class="keyword">return</span> minHeap;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> Node* <span class="title function_">buildHuffmanTree</span><span class="params">(<span class="type">int</span> values[], <span class="type">int</span> size)</span> &#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Node</span> *<span class="title">left</span>, *<span class="title">right</span>, *<span class="title">top</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">MinHeap</span>* <span class="title">minHeap</span> =</span> createAndBuildMinHeap(values, size);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (!isSizeOne(minHeap)) &#123;</span><br><span class="line">        left = extractMin(minHeap);</span><br><span class="line">        right = extractMin(minHeap);</span><br><span class="line"></span><br><span class="line">        top = newNode(left-&gt;value + right-&gt;value);</span><br><span class="line">        top-&gt;left = left;</span><br><span class="line">        top-&gt;right = right;</span><br><span class="line">        insertMinHeap(minHeap, top);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> extractMin(minHeap);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">printHuffmanSum</span><span class="params">(<span class="keyword">struct</span> Node* root, <span class="type">int</span> depth, <span class="type">int</span>* sum)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (!root)</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">        </span><br><span class="line">    <span class="keyword">if</span> (!root-&gt;left &amp;&amp; !root-&gt;right) &#123;</span><br><span class="line">        *sum += root-&gt;value * depth;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    printHuffmanSum(root-&gt;left, depth + <span class="number">1</span>, sum);</span><br><span class="line">    printHuffmanSum(root-&gt;right, depth + <span class="number">1</span>, sum);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">int</span> values[] = &#123;<span class="number">5</span>, <span class="number">9</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">16</span>, <span class="number">45</span>&#125;;</span><br><span class="line">    <span class="type">int</span> size = <span class="keyword">sizeof</span>(values)/<span class="keyword">sizeof</span>(values[<span class="number">0</span>]);</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Node</span>* <span class="title">root</span> =</span> buildHuffmanTree(values, size);</span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> sum = <span class="number">0</span>;</span><br><span class="line">    printHuffmanSum(root, <span class="number">0</span>, &amp;sum);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The sum of value times depth is: %d\n&quot;</span>, sum);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h1 id="动态规划"><a href="#动态规划" class="headerlink" title="动态规划"></a>动态规划</h1><h2 id="背包问题"><a href="#背包问题" class="headerlink" title="背包问题"></a>背包问题</h2><h3 id="01背包"><a href="#01背包" class="headerlink" title="01背包"></a>01背包</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 100005 </span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> c[N],w[N],f[N];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">max</span><span class="params">(<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> a&gt;b?a:b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> i,j,n,V;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;V,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;c[i],&amp;w[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= n; i++)&#123;</span><br><span class="line">		<span class="keyword">for</span> (j = V; j &gt;= <span class="number">0</span>; j--)</span><br><span class="line">        	<span class="keyword">if</span>(c[i]&lt;=j) f[j] = max(f[j], f[j - c[i]] + w[i]);</span><br><span class="line">	&#125; </span><br><span class="line">    </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,f[V]);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="完全背包"><a href="#完全背包" class="headerlink" title="完全背包"></a>完全背包</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 100005</span></span><br><span class="line"><span class="type">int</span> c[N],w[N],f[N];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">max</span><span class="params">(<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> a&gt;b?a:b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> i,j,n,V;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;V,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;c[i],&amp;w[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= n; i++)&#123;</span><br><span class="line">	    <span class="keyword">for</span> (j = c[i]; j &lt;= V; j++)</span><br><span class="line">	        f[j] = max(f[j], f[j - c[i]] + w[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,f[V]);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="最大上升子序列"><a href="#最大上升子序列" class="headerlink" title="最大上升子序列"></a>最大上升子序列</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAX 99999999</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> ar[MAX], br[MAX];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">Search</span><span class="params">(<span class="type">int</span> num, <span class="type">int</span> left, <span class="type">int</span> right)</span> &#123;</span><br><span class="line">	<span class="type">int</span> mid;</span><br><span class="line">	<span class="keyword">while</span> (left &lt;= right) &#123;</span><br><span class="line">		mid = (left + right) / <span class="number">2</span>;</span><br><span class="line">		<span class="keyword">if</span> (num &gt; br[mid])</span><br><span class="line">			left = mid + <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">			right = mid - <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> left;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//遇到一个比栈顶元素大的数，就放进栈里，</span></span><br><span class="line"><span class="comment">//遇到比栈顶元素小的就二分查找前边的元素，找到一个“最应该被换掉的元素”，用新数去更新前边的元素。</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">DP</span><span class="params">(<span class="type">int</span> n)</span> &#123;</span><br><span class="line">	<span class="type">int</span> i, len, pos;</span><br><span class="line">	br[<span class="number">1</span>] = ar[<span class="number">1</span>];</span><br><span class="line">	len = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">2</span>; i &lt;= n; i++) &#123;</span><br><span class="line">		<span class="keyword">if</span> (ar[i] &gt; br[len]) &#123;</span><br><span class="line">			br[++len] = ar[i];</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			pos = Search(ar[i], <span class="number">1</span>, len);</span><br><span class="line">			br[pos] = ar[i];</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> len;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span> &#123;</span><br><span class="line">	<span class="type">int</span> n, i;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;n);</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= n; i++)</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;ar[i]);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, DP(n));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="钢管切割问题"><a href="#钢管切割问题" class="headerlink" title="钢管切割问题"></a>钢管切割问题</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> a[<span class="number">11</span>] = &#123;<span class="number">0</span>,<span class="number">1</span>,<span class="number">5</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>,<span class="number">17</span>,<span class="number">17</span>,<span class="number">20</span>,<span class="number">24</span>,<span class="number">30</span>&#125;;</span><br><span class="line"><span class="type">int</span> Max[<span class="number">10000</span>];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">max</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span> &#123;</span><br><span class="line">	<span class="keyword">if</span>(a&gt;=b) &#123;</span><br><span class="line">		<span class="keyword">return</span> a;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> n;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		Max[i] = <span class="number">-999</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line"><span class="comment">//	int m = CUT_ROD_AUX(n);</span></span><br><span class="line">	<span class="type">int</span> m = BOTTOM_UP_CUT_ROD(n);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,m);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//自下而上</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">BOTTOM_UP_CUT_ROD</span><span class="params">(<span class="type">int</span> n)</span> &#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="type">int</span> q = <span class="number">-9999</span>;</span><br><span class="line">		<span class="type">int</span> j;</span><br><span class="line">		<span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=i;j++)&#123;</span><br><span class="line">			q = max(q,a[j]+Max[i-j]);</span><br><span class="line">		&#125;</span><br><span class="line">		Max[i] = q;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> Max[n];</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h2 id="流水线调度"><a href="#流水线调度" class="headerlink" title="流水线调度"></a>流水线调度</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">题目描述</span><br><span class="line">又到了一年一度校庆的时候。今年的校庆外场格外热闹，大大小小的摊位摆满了一整条路。每个专业都有两个外场摊位，分别位于道路的两端。每个摊位都会有一个小游戏，玩完游戏后可以获得一个印章，集齐所有的印章可以兑换一个校庆纪念大奖。</span><br><span class="line"></span><br><span class="line">Marvolo 和他的室友自然不会错过这个拿奖的机会。他们研究了一下规则后发现，一共有n个专业参加了活动，也就是有<span class="number">2</span>n个外场摊位。每个专业的两个摊位各持有印章的半个部分，路北的摊位有印章的左半部分，路南的有右半部分。印章的获取顺序必须是先获得左半部分印章，再获得右半部分印章。Marvolo 和室友商量好，他们一个人只逛路北的摊位，一个人只逛路南的摊位。一个人每次只能在一个摊位玩游戏。通过调查，他提前知道了玩完每个摊位的小游戏需要多少时间，现在只需要规划好一个逛摊位的方案，使得兑换大奖的时间最短（去的晚可能没了）。</span><br><span class="line"></span><br><span class="line">因为他们还要忙着去参加摊位的小游戏，所以找到了擅长编程的你，希望你可以帮忙解决这个问题。</span><br><span class="line"></span><br><span class="line">输入格式</span><br><span class="line">第一行，一个正整数n，表示专业数。</span><br><span class="line"></span><br><span class="line">接下来一行，有n个正整数，第i个正整数表示玩完第i个专业的路北摊位的小游戏需要花费多少时间。</span><br><span class="line"></span><br><span class="line">最后一行，有n个正整数，第i个正整数表示玩完第i个专业的路南摊位的小游戏需要花费多少时间。</span><br><span class="line"></span><br><span class="line">输出格式</span><br><span class="line">输出一个正整数，表示兑换大奖最短需要多久。</span><br></pre></td></tr></table></figure>



<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> Author: 林智灏</span></span><br><span class="line"><span class="comment"> Result: AC	Submission_id: 3667212</span></span><br><span class="line"><span class="comment"> Created at: Tue Oct 26 2021 23:11:37 GMT+0800 (China Standard Time)</span></span><br><span class="line"><span class="comment"> Problem_id: 4767	Time: 3	Memory: 1736</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span>&#123;</span></span><br><span class="line">	<span class="type">int</span> a;</span><br><span class="line">	<span class="type">int</span> b;</span><br><span class="line">&#125;TW;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">max</span><span class="params">(<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> a&gt;b?a:b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">TW tw[<span class="number">1005</span>],tw1[<span class="number">1005</span>],tw2[<span class="number">1005</span>];</span><br><span class="line"><span class="type">int</span> sum1[<span class="number">1005</span>],sum2[<span class="number">1005</span>];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">cmp</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *a, <span class="type">const</span> <span class="type">void</span> *b)</span>&#123;</span><br><span class="line">	TW *p = (TW *)a;</span><br><span class="line">	TW *q = (TW *)b;</span><br><span class="line">	<span class="keyword">return</span> q-&gt;b - p-&gt;b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">cmp2</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *a, <span class="type">const</span> <span class="type">void</span> *b)</span>&#123;</span><br><span class="line">	TW *p = (TW *)a;</span><br><span class="line">	TW *q = (TW *)b;</span><br><span class="line">	<span class="keyword">return</span> p-&gt;a - q-&gt;a;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> n;</span><br><span class="line">	<span class="type">int</span> i,j=<span class="number">0</span>,k=<span class="number">0</span>;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;tw[i].a);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;tw[i].b);</span><br><span class="line">		<span class="keyword">if</span>(tw[i].a&lt;tw[i].b)&#123;</span><br><span class="line">			tw1[++j] = tw[i];</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> tw2[++k] = tw[i];</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	qsort(tw1+<span class="number">1</span>,j,<span class="keyword">sizeof</span>(TW),cmp2);</span><br><span class="line">	qsort(tw2+<span class="number">1</span>,k,<span class="keyword">sizeof</span>(TW),cmp);</span><br><span class="line">	</span><br><span class="line">	sum1[<span class="number">0</span>] = <span class="number">0</span>, sum2[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=j;i++)&#123;</span><br><span class="line">		sum1[i] = sum1[i<span class="number">-1</span>] + tw1[i].a;</span><br><span class="line">		sum2[i] = max(sum1[i],sum2[i<span class="number">-1</span>]) + tw1[i].b;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=k;i++)&#123;</span><br><span class="line">		sum1[i+j] = sum1[i+j<span class="number">-1</span>] + tw2[i].a;</span><br><span class="line">		sum2[i+j] = max(sum1[i+j],sum2[i+j<span class="number">-1</span>]) + tw2[i].b;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,sum2[n]);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="矩阵链乘法"><a href="#矩阵链乘法" class="headerlink" title="矩阵链乘法"></a>矩阵链乘法</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> M 305</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INT_MAX 99999999</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> m[M][M],p[M], s[M][M], m2[M][M], s2[M][M];</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">MATRIX_CHAIN_ORDER</span><span class="params">(<span class="type">int</span> Length)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> q,n=Length;</span><br><span class="line">	<span class="type">int</span> i,l; </span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++) m[i][i]=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span>(l=<span class="number">2</span>;l&lt;=n;l++) 	<span class="comment">/* 矩阵链的长度 */</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n-l+<span class="number">1</span>;i++) </span><br><span class="line">		&#123;</span><br><span class="line">			<span class="type">int</span> j=i+l<span class="number">-1</span>;         <span class="comment">/* 等价于 l=j-i+1 */</span></span><br><span class="line">			<span class="comment">// 最小的 </span></span><br><span class="line">			m[i][j]=INT_MAX;</span><br><span class="line">			<span class="keyword">for</span>(<span class="type">int</span> k=i;k&lt;=j<span class="number">-1</span>;k++)</span><br><span class="line">			&#123;</span><br><span class="line">				q=m[i][k]+m[k+<span class="number">1</span>][j]+p[i<span class="number">-1</span>]*p[k]*p[j];</span><br><span class="line">				<span class="keyword">if</span>(q&lt;m[i][j])</span><br><span class="line">				&#123;</span><br><span class="line">					m[i][j]=q;</span><br><span class="line">					s[i][j]=k;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">			<span class="comment">// 最大的</span></span><br><span class="line">			</span><br><span class="line">			m2[i][j]=-INT_MAX;</span><br><span class="line">			<span class="keyword">for</span>(<span class="type">int</span> k=i;k&lt;=j<span class="number">-1</span>;k++)</span><br><span class="line">			&#123;</span><br><span class="line">				q=m2[i][k]+m2[k+<span class="number">1</span>][j]+p[i<span class="number">-1</span>]*p[k]*p[j];</span><br><span class="line">				<span class="keyword">if</span>(q&gt;m2[i][j])</span><br><span class="line">				&#123;</span><br><span class="line">					m2[i][j]=q;</span><br><span class="line">					s2[i][j]=k;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125; </span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">PRINT_MIN_OPTIMAL_PARENS</span><span class="params">(<span class="type">int</span> i,<span class="type">int</span> j)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span>(i == j) <span class="built_in">printf</span>(<span class="string">&quot;A%d&quot;</span>,i);</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;(&quot;</span>);</span><br><span class="line">		PRINT_MIN_OPTIMAL_PARENS(i,s[i][j]);</span><br><span class="line">		PRINT_MIN_OPTIMAL_PARENS(s[i][j]+<span class="number">1</span>,j);</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;)&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">PRINT_MAX_OPTIMAL_PARENS</span><span class="params">(<span class="type">int</span> i,<span class="type">int</span> j)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span>(i == j) <span class="built_in">printf</span>(<span class="string">&quot;A%d&quot;</span>,i);</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;(&quot;</span>);</span><br><span class="line">		PRINT_MAX_OPTIMAL_PARENS(i,s2[i][j]);</span><br><span class="line">		PRINT_MAX_OPTIMAL_PARENS(s2[i][j]+<span class="number">1</span>,j);</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;)&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="type">int</span> n;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;p[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line"></span><br><span class="line">	MATRIX_CHAIN_ORDER(n);</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,m[<span class="number">1</span>][n]);</span><br><span class="line">	PRINT_MIN_OPTIMAL_PARENS(<span class="number">1</span>,n);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,m2[<span class="number">1</span>][n]);</span><br><span class="line">	PRINT_MAX_OPTIMAL_PARENS(<span class="number">1</span>,n);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// </span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="最长公共子序列"><a href="#最长公共子序列" class="headerlink" title="最长公共子序列"></a>最长公共子序列</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> c[<span class="number">1005</span>][<span class="number">1005</span>];</span><br><span class="line"><span class="type">int</span> b[<span class="number">1005</span>][<span class="number">1005</span>];</span><br><span class="line"><span class="type">char</span> s1[<span class="number">1005</span>], s2[<span class="number">1005</span>];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">max</span><span class="params">(<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> a&gt;b?a:b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">PRINT_LCS</span><span class="params">(<span class="type">int</span> i,<span class="type">int</span> j)</span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(i == <span class="number">0</span> || j == <span class="number">0</span>)&#123;</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(b[i][j] == <span class="number">2</span>)&#123;</span><br><span class="line">		PRINT_LCS(i<span class="number">-1</span>,j<span class="number">-1</span>);</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>,s1[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (b[i][j] == <span class="number">1</span>) &#123;</span><br><span class="line">		PRINT_LCS(i<span class="number">-1</span>,j);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> PRINT_LCS(i,j<span class="number">-1</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> k;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;k);</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>,s1+<span class="number">1</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>,s2+<span class="number">1</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="type">int</span> i,j;</span><br><span class="line">	<span class="type">int</span> len1 = <span class="built_in">strlen</span>(s1+<span class="number">1</span>),len2 = <span class="built_in">strlen</span>(s2+<span class="number">1</span>);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=len1;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=len2;j++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(s1[i] == s2[j])&#123;</span><br><span class="line">				c[i][j] = c[i<span class="number">-1</span>][j<span class="number">-1</span>]+<span class="number">1</span>;</span><br><span class="line">				b[i][j] = <span class="number">2</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span>&#123;</span><br><span class="line">				<span class="keyword">if</span>(c[i<span class="number">-1</span>][j]&gt;=c[i][j<span class="number">-1</span>])&#123;</span><br><span class="line">					b[i][j] = <span class="number">1</span>;</span><br><span class="line">					c[i][j] = c[i<span class="number">-1</span>][j];</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span>&#123;</span><br><span class="line">					b[i][j] = <span class="number">3</span>;</span><br><span class="line">					c[i][j] = c[i][j<span class="number">-1</span>];</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(k==<span class="number">0</span>) <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,c[len1][len2]);</span><br><span class="line">	<span class="keyword">else</span>&#123;</span><br><span class="line">		PRINT_LCS(len1,len2);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="最长公共子串"><a href="#最长公共子串" class="headerlink" title="最长公共子串"></a>最长公共子串</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> c[<span class="number">1005</span>][<span class="number">1005</span>];</span><br><span class="line"><span class="type">int</span> b[<span class="number">1005</span>][<span class="number">1005</span>];</span><br><span class="line"><span class="type">char</span> s1[<span class="number">1005</span>], s2[<span class="number">1005</span>];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">max</span><span class="params">(<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> a&gt;b?a:b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">PRINT_LCS</span><span class="params">(<span class="type">int</span> i,<span class="type">int</span> j)</span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(i == <span class="number">0</span> || j == <span class="number">0</span> || s1[i] != s2[j])&#123;</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	PRINT_LCS(i<span class="number">-1</span>,j<span class="number">-1</span>);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>,s1[i]);</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> k;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>,s1+<span class="number">1</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>,s2+<span class="number">1</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="type">int</span> i,j;</span><br><span class="line">	<span class="type">int</span> len1 = <span class="built_in">strlen</span>(s1+<span class="number">1</span>),len2 = <span class="built_in">strlen</span>(s2+<span class="number">1</span>);</span><br><span class="line">	<span class="type">int</span> max=<span class="number">0</span>,flagx,flagy;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=len1;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=len2;j++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(s1[i] == s2[j])&#123;</span><br><span class="line">				c[i][j] = c[i<span class="number">-1</span>][j<span class="number">-1</span>]+<span class="number">1</span>;</span><br><span class="line">				<span class="keyword">if</span>(c[i][j]&gt;max)&#123;</span><br><span class="line">					max = c[i][j];</span><br><span class="line">					flagx=i;</span><br><span class="line">					flagy=j;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span>&#123;</span><br><span class="line">				c[i][j] = <span class="number">0</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,c[flagx][flagy]);</span><br><span class="line">	PRINT_LCS(flagx,flagy);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="最优二叉搜索树"><a href="#最优二叉搜索树" class="headerlink" title="最优二叉搜索树"></a>最优二叉搜索树</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DBL_MAX 999999999</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> n=<span class="number">5</span>; </span><br><span class="line"></span><br><span class="line"><span class="type">double</span> p[<span class="number">6</span>]=&#123;<span class="number">0</span>,<span class="number">0.15</span>,<span class="number">0.10</span>,<span class="number">0.05</span>,<span class="number">0.10</span>,<span class="number">0.20</span>&#125;;</span><br><span class="line"><span class="type">double</span> q[<span class="number">6</span>]=&#123;<span class="number">0.05</span> ,<span class="number">0.10</span> ,<span class="number">0.05</span> ,<span class="number">0.05</span> ,<span class="number">0.05</span> ,<span class="number">0.10</span>&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 下标范围：e[1..n+1,0..n]  w[1..n+1,0..n] root[1..n,1..n] */</span></span><br><span class="line"><span class="type">double</span> e[<span class="number">6</span>][<span class="number">6</span>],w[<span class="number">6</span>][<span class="number">6</span>];</span><br><span class="line"><span class="type">int</span> root[<span class="number">6</span>][<span class="number">6</span>];</span><br><span class="line"></span><br><span class="line"><span class="type">double</span> <span class="title function_">OPTIMAL_BST</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i,l;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n+<span class="number">1</span>;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		e[i][i<span class="number">-1</span>]=q[i<span class="number">-1</span>];</span><br><span class="line">		w[i][i<span class="number">-1</span>]=q[i<span class="number">-1</span>];</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(l=<span class="number">1</span>;l&lt;=n;l++)         <span class="comment">/* 类似于矩阵链乘法 */</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n-l+<span class="number">1</span>;i++)</span><br><span class="line">		&#123;	</span><br><span class="line">			<span class="type">int</span> j=i+l<span class="number">-1</span>,r;</span><br><span class="line">			e[i][j]=DBL_MAX ;</span><br><span class="line">			w[i][j]=w[i][j<span class="number">-1</span>]+p[j]+q[j];</span><br><span class="line">			<span class="keyword">for</span>(r=i;r&lt;=j;r++)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="type">double</span> t=e[i][r<span class="number">-1</span>]+e[r+<span class="number">1</span>][j]+w[i][j];</span><br><span class="line">				</span><br><span class="line">				<span class="keyword">if</span>(t&lt;e[i][j])</span><br><span class="line">				&#123;</span><br><span class="line">					e[i][j]=t;</span><br><span class="line">					root[i][j]=r;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> e[<span class="number">1</span>][n];</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">void</span> <span class="title function_">CONSTRUCT_OPTIAML_BST</span><span class="params">(<span class="type">int</span> i,<span class="type">int</span> j)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span>(i==<span class="number">1</span> &amp;&amp; j==n)<span class="built_in">printf</span>(<span class="string">&quot;K%d为根\n&quot;</span>,root[i][j]);</span><br><span class="line">	<span class="keyword">if</span>(i &lt; j)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;K%d是K%d的左孩子\n&quot;</span>,root[i][root[i][j]<span class="number">-1</span>],root[i][j]);</span><br><span class="line">		CONSTRUCT_OPTIAML_BST(i,root[i][j]<span class="number">-1</span>);</span><br><span class="line">		<span class="keyword">if</span>(root[i][j]+<span class="number">1</span> &lt; j) <span class="built_in">printf</span>(<span class="string">&quot;K%d是K%d的右孩子\n&quot;</span>,root[root[i][j]+<span class="number">1</span>][j],root[i][j]);</span><br><span class="line">		CONSTRUCT_OPTIAML_BST(root[i][j]+<span class="number">1</span>,j);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(i == j)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;d%d是K%d的左孩子\n&quot;</span>,i<span class="number">-1</span>,i);</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;d%d是K%d的右孩子\n&quot;</span>,i,i);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(i &gt; j) <span class="built_in">printf</span>(<span class="string">&quot;d%d是K%d的右孩子\n&quot;</span>,j,j);</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;最优期望搜索代价：%.4lf\n&quot;</span>,OPTIMAL_BST(n));</span><br><span class="line"></span><br><span class="line">	CONSTRUCT_OPTIAML_BST(<span class="number">1</span>,n);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="加分二叉树"><a href="#加分二叉树" class="headerlink" title="加分二叉树"></a>加分二叉树</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstring&gt;</span></span></span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> MAXN = <span class="number">50</span>;</span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line">ll n;</span><br><span class="line">ll f[MAXN][MAXN], root[MAXN][MAXN];</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">print</span><span class="params">(ll l, ll r)</span> &#123;</span><br><span class="line">	<span class="keyword">if</span> (l &gt; r)<span class="keyword">return</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%lld &quot;</span>, root[l][r]);</span><br><span class="line">	<span class="keyword">if</span> (l == r)<span class="keyword">return</span>;</span><br><span class="line">	print(l, root[l][r] - <span class="number">1</span>);</span><br><span class="line">	print(root[l][r]+<span class="number">1</span>,r);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span> &#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%lld&quot;</span>, &amp;n);</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt;= n; i++)<span class="built_in">scanf</span>(<span class="string">&quot;%lld&quot;</span>, &amp;f[i][i]),f[i][i<span class="number">-1</span>]=<span class="number">1</span>, root[i][i] = i;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> len = <span class="number">1</span>; len &lt; n; ++len) &#123;</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i + len &lt;= n; ++i) &#123;</span><br><span class="line">			<span class="type">int</span> j = i + len;</span><br><span class="line">			f[i][j] = f[i + <span class="number">1</span>][j] + f[i][i];<span class="comment">//默认它的左子树为空，如果有的话，这肯定不是最优解</span></span><br><span class="line">			root[i][j] = i;<span class="comment">//默认从起点选根</span></span><br><span class="line">			<span class="keyword">for</span> (<span class="type">int</span> k = i + <span class="number">1</span>; k &lt; j; ++k) &#123;</span><br><span class="line">				<span class="keyword">if</span> (f[i][j] &lt; f[i][k - <span class="number">1</span>] * f[k + <span class="number">1</span>][j] + f[k][k]) &#123;</span><br><span class="line">					f[i][j] = f[i][k - <span class="number">1</span>] * f[k + <span class="number">1</span>][j] + f[k][k];</span><br><span class="line">					root[i][j] = k;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; f[<span class="number">1</span>][n] &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	print(<span class="number">1</span>, n);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="图论"><a href="#图论" class="headerlink" title="图论"></a>图论</h1><h2 id="基本图算法"><a href="#基本图算法" class="headerlink" title="基本图算法"></a>基本图算法</h2><h3 id="广度优先搜索"><a href="#广度优先搜索" class="headerlink" title="广度优先搜索"></a>广度优先搜索</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">note</span>&#123;</span></span><br><span class="line">	<span class="type">int</span> x;<span class="comment">//城市编号</span></span><br><span class="line">	<span class="type">int</span> s;<span class="comment">//转机次数 </span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> book[<span class="number">101</span>],e[<span class="number">101</span>][<span class="number">101</span>],n,m,head,tail,i,j,start,end,flag=<span class="number">0</span>;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">note</span> <span class="title">que</span>[10001];</span></span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d %d %d&quot;</span>,&amp;n,&amp;m,&amp;start,&amp;end);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">			e[i][j]=<span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		book[i]=<span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=m;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;head,&amp;tail);</span><br><span class="line">		e[head][tail]=<span class="number">1</span>;</span><br><span class="line">		e[tail][head]=<span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	que[<span class="number">1</span>].x=start;</span><br><span class="line">	que[<span class="number">1</span>].s=<span class="number">0</span>;</span><br><span class="line">	book[start]=<span class="number">1</span>;</span><br><span class="line">	head=<span class="number">1</span>;</span><br><span class="line">	tail=<span class="number">2</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(head&lt;tail)&#123;</span><br><span class="line">		<span class="type">int</span> cur=que[head].x;</span><br><span class="line">		<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">			<span class="keyword">if</span> (book[i]==<span class="number">0</span>&amp;&amp;e[cur][i]==<span class="number">1</span>)&#123;</span><br><span class="line">				que[tail].x=i;</span><br><span class="line">				que[tail].s=que[head].s+<span class="number">1</span>;</span><br><span class="line">				tail++;</span><br><span class="line">				book[i]=<span class="number">1</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span>(que[tail].x==end)&#123;</span><br><span class="line">				flag=<span class="number">1</span>;</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(flag==<span class="number">1</span>) <span class="keyword">break</span>;</span><br><span class="line">		head++;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,que[tail<span class="number">-1</span>].s);</span><br><span class="line">	getchar();getchar();</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="图的深度优先搜索"><a href="#图的深度优先搜索" class="headerlink" title="图的深度优先搜索"></a>图的深度优先搜索</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> e[<span class="number">101</span>][<span class="number">101</span>],book[<span class="number">101</span>],sum=<span class="number">0</span>,n,m;</span><br><span class="line"><span class="type">void</span> <span class="title function_">dfs</span><span class="params">(<span class="type">int</span> cur)</span>&#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>,cur);</span><br><span class="line">	sum++;</span><br><span class="line">	<span class="keyword">if</span> (sum==n) <span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="keyword">if</span> (e[cur][i]==<span class="number">1</span> &amp;&amp; book[i]==<span class="number">0</span>)&#123;</span><br><span class="line">			book[i]=<span class="number">1</span>;</span><br><span class="line">			dfs(i);</span><br><span class="line">			<span class="keyword">if</span> (sum==n) <span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> i,j,a,b;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入点的个数和边的条数：&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;n,&amp;m);</span><br><span class="line">	<span class="keyword">for</span> (i=<span class="number">1</span>;i&lt;=<span class="number">100</span>;i++) &#123;</span><br><span class="line">		<span class="keyword">for</span> (j=<span class="number">1</span>;j&lt;=<span class="number">100</span>;j++) &#123;</span><br><span class="line">			e[i][j] = <span class="number">-1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		book[i] = <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">			<span class="keyword">if</span> (i==j) e[i][j]=<span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=m;i++)&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;请输入相连两点编号：&quot;</span>);</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;a,&amp;b); </span><br><span class="line">		e[a][b]=<span class="number">1</span>;</span><br><span class="line">		e[b][a]=<span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	book[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line">	dfs(<span class="number">1</span>);</span><br><span class="line">	</span><br><span class="line">	getchar(); getchar();</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h3 id="拓扑排序"><a href="#拓扑排序" class="headerlink" title="拓扑排序"></a>拓扑排序</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"></span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll; </span><br><span class="line"></span><br><span class="line"><span class="built_in">priority_queue</span>&lt;<span class="type">int</span>&gt; q;</span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="type">int</span>&gt; E[<span class="number">100005</span>];</span><br><span class="line"><span class="type">int</span> In[<span class="number">100005</span>];</span><br><span class="line"><span class="type">int</span> book[<span class="number">100005</span>];</span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="type">int</span>&gt; Topo;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="title function_">cmp</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> x &gt; y;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> n,m;</span><br><span class="line">	<span class="built_in">cin</span>&gt;&gt;n&gt;&gt;m;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=m;i++)&#123;</span><br><span class="line">		<span class="type">int</span> u,v;</span><br><span class="line">		<span class="built_in">cin</span>&gt;&gt;u&gt;&gt;v;</span><br><span class="line">		E[u].push_back(v);</span><br><span class="line">		In[v] += <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="type">int</span> flag = <span class="number">0</span>;	 </span><br><span class="line">    <span class="keyword">for</span>(i=n;i&gt;=<span class="number">1</span>;i--) &#123;</span><br><span class="line">    	<span class="keyword">if</span>(In[i]==<span class="number">0</span>)&#123;</span><br><span class="line">    		flag = <span class="number">1</span>;</span><br><span class="line">    		q.push(i); </span><br><span class="line">		&#125; </span><br><span class="line">    	sort(E[i].begin(),E[i].end(),cmp);</span><br><span class="line">	&#125; </span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(flag==<span class="number">0</span>) &#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;YE5&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">    <span class="keyword">while</span>(!q.empty())</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="type">int</span> x=q.top(); </span><br><span class="line">		q.pop(); </span><br><span class="line">        Topo.push_back(x);</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;E[x].size();i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="type">int</span> y = E[x][i];</span><br><span class="line">            In[y]--;</span><br><span class="line">            <span class="keyword">if</span>(In[y]==<span class="number">0</span>)</span><br><span class="line">                q.push(y);  </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(q.empty() &amp;&amp; Topo.size()!=n)&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;YE5&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;N0&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="最小生成树"><a href="#最小生成树" class="headerlink" title="最小生成树"></a>最小生成树</h2><h3 id="并查集"><a href="#并查集" class="headerlink" title="并查集"></a>并查集</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> f[<span class="number">1000</span>]=&#123;<span class="number">0</span>&#125;,n,m,k,sum=<span class="number">0</span>;</span><br><span class="line"><span class="comment">//初始化 </span></span><br><span class="line"><span class="type">void</span> <span class="title function_">init</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++) f[i]=i;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 不断地找爹，直到找到最高领导人（f[v]=v）</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">getf</span><span class="params">(<span class="type">int</span> v)</span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(f[v]==v) <span class="keyword">return</span> v;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		f[v]=getf(f[v]);</span><br><span class="line">		<span class="keyword">return</span> f[v];</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//合并.当这俩点有边的时候，若他们不属于一个爹，则让他们有同一个爹</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">merge</span><span class="params">(<span class="type">int</span> x,<span class="type">int</span> y)</span>&#123;</span><br><span class="line">	<span class="type">int</span> t1,t2;</span><br><span class="line">	t1=getf(x);</span><br><span class="line">	t2=getf(y);</span><br><span class="line">	<span class="keyword">if</span>(t1!=t2)&#123;</span><br><span class="line">		f[t2]=t1;</span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;n,&amp;m);</span><br><span class="line">	</span><br><span class="line">	init();</span><br><span class="line">	<span class="type">int</span> x,y,i;</span><br><span class="line">    <span class="comment">// 这里是输入边，每输入一条边，进行一次认爹的过程</span></span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=m;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;x,&amp;y);</span><br><span class="line">		merge(x,y);</span><br><span class="line">	&#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 看看有多少个最高级别的爹</span></span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="keyword">if</span> (f[i]==i) sum++;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,sum);</span><br><span class="line">	getchar();getchar();</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="最小生成树-1"><a href="#最小生成树-1" class="headerlink" title="最小生成树"></a>最小生成树</h3><h4 id="Kruskal（稀疏图-O-ElogV-）"><a href="#Kruskal（稀疏图-O-ElogV-）" class="headerlink" title="Kruskal（稀疏图$O(ElogV$)）"></a>Kruskal（稀疏图$O(ElogV$)）</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span>&#123;</span></span><br><span class="line">	<span class="type">int</span> u;</span><br><span class="line">	<span class="type">int</span> v;</span><br><span class="line">	<span class="type">double</span> w;</span><br><span class="line">&#125;Edge;</span><br><span class="line"></span><br><span class="line">Edge e[<span class="number">1000005</span>];</span><br><span class="line"><span class="type">int</span> n,m;</span><br><span class="line"><span class="type">int</span> f[<span class="number">1000005</span>]=&#123;<span class="number">0</span>&#125;,count=<span class="number">0</span>;</span><br><span class="line"><span class="type">double</span> sum=<span class="number">0</span>; </span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span>&#123;</span></span><br><span class="line">	<span class="type">int</span> x;</span><br><span class="line">	<span class="type">int</span> y;</span><br><span class="line">&#125;point[<span class="number">1005</span>];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">cmp</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *a, <span class="type">const</span> <span class="type">void</span> *b)</span>&#123;</span><br><span class="line">	Edge *p = (Edge *)a;</span><br><span class="line">	Edge *q = (Edge *)b;</span><br><span class="line">	<span class="keyword">if</span>(p-&gt;w &gt; q-&gt;w) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//找祖先</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">getf</span><span class="params">(<span class="type">int</span> v)</span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (f[v]==v) <span class="keyword">return</span> v;</span><br><span class="line">	<span class="keyword">else</span>&#123;</span><br><span class="line">		f[v]=getf(f[v]);</span><br><span class="line">		<span class="keyword">return</span> f[v];</span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//并查集合</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">merge</span><span class="params">(<span class="type">int</span> v,<span class="type">int</span> u)</span>&#123;</span><br><span class="line">	<span class="type">int</span> t1,t2;</span><br><span class="line">	t1=getf(v);</span><br><span class="line">	t2=getf(u);</span><br><span class="line">	<span class="keyword">if</span>(t1!=t2)&#123;</span><br><span class="line">		f[t2]=t1;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> i,j=<span class="number">1</span>,k=<span class="number">1</span>;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;point[i].x,&amp;point[i].y);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(i&lt;j)&#123;</span><br><span class="line">				e[k].u = i;</span><br><span class="line">				e[k].v = j;</span><br><span class="line">				e[k].w = <span class="built_in">pow</span>((point[i].x-point[j].x)*(point[i].x-point[j].x)+(point[i].y-point[j].y)*(point[i].y-point[j].y),<span class="number">0.5</span>);</span><br><span class="line">				k++;</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	k--;</span><br><span class="line">	qsort(e+<span class="number">1</span>,k,<span class="keyword">sizeof</span>(Edge),cmp);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++) f[i]=i;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=k;i++)&#123;</span><br><span class="line">		<span class="keyword">if</span> (merge(e[i].u,e[i].v))&#123;</span><br><span class="line">			count++;</span><br><span class="line">			sum+=e[i].w;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(count==n<span class="number">-1</span>) <span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%.2lf&quot;</span>,sum);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="Prim最小生成树（-稠密图O-V-2-）"><a href="#Prim最小生成树（-稠密图O-V-2-）" class="headerlink" title="Prim最小生成树（$稠密图O(V^2)$）"></a>Prim最小生成树（$稠密图O(V^2)$）</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INF 99999</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> e[<span class="number">105</span>][<span class="number">105</span>];</span><br><span class="line"><span class="type">int</span> book[<span class="number">105</span>]; </span><br><span class="line"><span class="type">int</span> dis[<span class="number">105</span>];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> n,m; <span class="comment">//n表示点数，m表示边数</span></span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;n,&amp;m);</span><br><span class="line">	<span class="type">int</span> i,j;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(i == j) e[i][j] = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">else</span> e[i][j] = INF;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125; </span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=m;i++)&#123;</span><br><span class="line">		<span class="type">int</span> b,c,d;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d %d %d&quot;</span>,&amp;b, &amp;c, &amp;d);	</span><br><span class="line">		e[b][c] = d;</span><br><span class="line">		e[c][b] = d;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++) dis[i]=e[<span class="number">1</span>][i];	</span><br><span class="line">	<span class="type">int</span> cnt = <span class="number">1</span>, sum = <span class="number">0</span>;</span><br><span class="line">	book[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(cnt &lt; n) &#123;</span><br><span class="line">		<span class="type">int</span> min = INF;</span><br><span class="line">		<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;					<span class="comment">//找出队列中未遍历的权值最小的点 </span></span><br><span class="line">			<span class="keyword">if</span>(book[i]==<span class="number">0</span>&amp;&amp;dis[i]&lt;min)&#123;</span><br><span class="line">				min=dis[i];</span><br><span class="line">				j=i;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		book[j]=<span class="number">1</span>;     <span class="comment">//遍历该点 </span></span><br><span class="line">		cnt++;</span><br><span class="line">		sum+=dis[j];	</span><br><span class="line">		<span class="type">int</span> k;</span><br><span class="line">		<span class="keyword">for</span>(k=<span class="number">1</span>;k&lt;=n;k++)&#123;						<span class="comment">//若与该点相连的点的距离与该点权值相加小于队列中的权值，则替换 </span></span><br><span class="line">			<span class="keyword">if</span>(book[k]==<span class="number">0</span>&amp;&amp;dis[k]&gt;e[j][k])&#123;</span><br><span class="line">				dis[k]=e[j][k];</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="最小生成树加边称完全树"><a href="#最小生成树加边称完全树" class="headerlink" title="最小生成树加边称完全树"></a>最小生成树加边称完全树</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 20050</span></span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">long</span> <span class="type">long</span> pre[N];</span><br><span class="line"><span class="type">long</span> <span class="type">long</span> num[N]=&#123;<span class="number">0</span>&#125;;</span><br><span class="line"><span class="type">void</span> <span class="title function_">init</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">long</span> <span class="type">long</span> i=<span class="number">0</span>;i&lt;N;i++)pre[i]=i,num[i]=<span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">long</span> <span class="type">long</span> <span class="title function_">Find</span><span class="params">(<span class="type">long</span> <span class="type">long</span> x)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">long</span> <span class="type">long</span> boss=x;</span><br><span class="line">    <span class="keyword">while</span>(pre[boss]!=boss)boss=pre[boss];</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">while</span>(x!=boss)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="type">long</span> <span class="type">long</span> now=pre[x];</span><br><span class="line">        pre[x]=boss;</span><br><span class="line">        x=now;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> boss;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">ss</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">long</span> <span class="type">long</span> u,v,w;</span><br><span class="line">    </span><br><span class="line">    <span class="type">bool</span> operator &lt; (<span class="type">const</span> ss&amp;s) <span class="type">const</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> w&lt;s.w;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">ss edge[N];</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">long</span> <span class="type">long</span> n;</span><br><span class="line">    init();</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%lld&quot;</span>,&amp;n);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">long</span> <span class="type">long</span> i=<span class="number">1</span>;i&lt;n;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%lld %lld %lld&quot;</span>,&amp;edge[i].u,&amp;edge[i].v,&amp;edge[i].w);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    sort(edge+<span class="number">1</span>,edge+n);</span><br><span class="line">    </span><br><span class="line">    <span class="type">long</span> <span class="type">long</span> ans=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">long</span> <span class="type">long</span> i=<span class="number">1</span>;i&lt;n;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="type">long</span> <span class="type">long</span> u=edge[i].u,v=edge[i].v;</span><br><span class="line">        ans+=(edge[i].w+<span class="number">1</span>)*(num[Find(u)]*num[Find(v)]<span class="number">-1</span>);</span><br><span class="line">        ans+=edge[i].w;</span><br><span class="line">        num[Find(v)]+=num[Find(u)];</span><br><span class="line">        pre[Find(u)]=Find(v);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%lld\n&quot;</span>,ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="最小生成树逆向生成完全图"><a href="#最小生成树逆向生成完全图" class="headerlink" title="最小生成树逆向生成完全图"></a>最小生成树逆向生成完全图</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span>&#123;</span></span><br><span class="line">	<span class="type">int</span> x;</span><br><span class="line">	<span class="type">int</span> y;</span><br><span class="line">	<span class="type">int</span> v;</span><br><span class="line">&#125;Edge;</span><br><span class="line"></span><br><span class="line">Edge edge[<span class="number">100005</span>];</span><br><span class="line"><span class="type">int</span> pre[<span class="number">100005</span>];</span><br><span class="line"><span class="type">int</span> num[<span class="number">100005</span>];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">cmp</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *a, <span class="type">const</span> <span class="type">void</span> *b)</span>&#123;</span><br><span class="line">	Edge p = *(Edge*)a;</span><br><span class="line">	Edge q = *(Edge*)b;</span><br><span class="line">	<span class="keyword">return</span> p.v-q.v;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">Find</span><span class="params">(<span class="type">int</span> x)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> boss=x;</span><br><span class="line">    <span class="keyword">while</span>(pre[boss]!=boss)boss=pre[boss];</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">while</span>(x!=boss)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="type">int</span> now=pre[x];</span><br><span class="line">        pre[x]=boss;</span><br><span class="line">        x=now;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> boss;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> t;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;t);</span><br><span class="line">	<span class="keyword">while</span>(t--)&#123;</span><br><span class="line">		<span class="type">int</span> n;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">		<span class="type">int</span> i;</span><br><span class="line">		<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">			pre[i]=i;</span><br><span class="line">			num[i]=<span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;n;i++)&#123;</span><br><span class="line">			<span class="built_in">scanf</span>(<span class="string">&quot;%d %d %d&quot;</span>,&amp;edge[i].x,&amp;edge[i].y,&amp;edge[i].v);</span><br><span class="line">		&#125;</span><br><span class="line">		qsort(edge+<span class="number">1</span>,n<span class="number">-1</span>,<span class="keyword">sizeof</span>(edge[<span class="number">1</span>]),cmp);</span><br><span class="line">		ll ans = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;n;i++)</span><br><span class="line">	    &#123;</span><br><span class="line">	        <span class="type">int</span> u=edge[i].x,v=edge[i].y;</span><br><span class="line">	        ans+=(edge[i].v+<span class="number">1</span>)*(ll)(num[Find(u)]*num[Find(v)]<span class="number">-1</span>);</span><br><span class="line">	        ans+=edge[i].v;</span><br><span class="line">	        num[Find(v)]+=num[Find(u)];</span><br><span class="line">	        pre[Find(u)]=Find(v);</span><br><span class="line">	    &#125;	</span><br><span class="line">	    <span class="built_in">printf</span>(<span class="string">&quot;%lld\n&quot;</span>,ans);</span><br><span class="line">	&#125; </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="最短路径"><a href="#最短路径" class="headerlink" title="最短路径"></a>最短路径</h2><h4 id="Bellman-Ford"><a href="#Bellman-Ford" class="headerlink" title="Bellman-Ford"></a>Bellman-Ford</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> Inf 99999999 <span class="comment">// 定义一个表示无穷大的常量</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> dis[<span class="number">10</span>],i,k,m,n,u[<span class="number">10</span>],v[<span class="number">10</span>],w[<span class="number">10</span>],dis2[<span class="number">10</span>];</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;n,&amp;m); <span class="comment">// 输入顶点数 n 和边数 m</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 输入边的起点 u[i]、终点 v[i] 和权重 w[i]</span></span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d %d %d&quot;</span>,&amp;u[i],&amp;v[i],&amp;w[i]);</span><br><span class="line">        dis[i]=Inf; <span class="comment">// 初始化 dis 数组，表示距离为无穷大</span></span><br><span class="line">        dis2[i]=Inf; <span class="comment">// 初始化 dis2 数组，用于检测负权回路</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    dis[<span class="number">1</span>]=<span class="number">0</span>; <span class="comment">// 起点到自身的距离为 0</span></span><br><span class="line">    dis2[<span class="number">1</span>]=<span class="number">0</span>; <span class="comment">// 起点到自身的距离为 0</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 进行 n-1 次循环，使用 Bellman-Ford 算法计算最短路径</span></span><br><span class="line">    <span class="keyword">for</span>(k=<span class="number">1</span>;k&lt;=n<span class="number">-1</span>;k++)&#123;</span><br><span class="line">        <span class="type">int</span> flag=<span class="number">1</span>; <span class="comment">// 用于标记是否所有顶点的最短路径都已经稳定</span></span><br><span class="line">        <span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=m;i++)&#123;</span><br><span class="line">            <span class="keyword">if</span> (dis[v[i]]&gt;dis[u[i]]+w[i]) &#123;</span><br><span class="line">                dis[v[i]]=dis[u[i]]+w[i]; <span class="comment">// 更新最短路径</span></span><br><span class="line">            &#125;            </span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span>(i=<span class="number">2</span>;i&lt;=m;i++)&#123;</span><br><span class="line">            <span class="keyword">if</span> (dis[v[i]]!=dis2[v[i<span class="number">-1</span>]]) &#123;</span><br><span class="line">                flag=<span class="number">0</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (flag==<span class="number">1</span>) <span class="keyword">break</span>; <span class="comment">// 如果所有顶点的最短路径都稳定，跳出循环</span></span><br><span class="line">        dis2[v[i]]=dis[u[i]]+w[i]; <span class="comment">// 更新 dis2 数组</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="type">int</span> flag=<span class="number">0</span>;</span><br><span class="line">    <span class="comment">// 检查是否存在负权回路</span></span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        <span class="keyword">if</span> (dis[v[i]]&gt;dis[u[i]]+w[i])&#123;</span><br><span class="line">            flag=<span class="number">1</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;   </span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据是否存在负权回路进行输出</span></span><br><span class="line">    <span class="keyword">if</span>(flag==<span class="number">1</span>) <span class="built_in">printf</span>(<span class="string">&quot;有负权回路&quot;</span>);</span><br><span class="line">    <span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>,dis[i]); <span class="comment">// 输出最短路径</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    getchar();</span><br><span class="line">    getchar();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="Dijkstra"><a href="#Dijkstra" class="headerlink" title="Dijkstra"></a>Dijkstra</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> e[<span class="number">101</span>][<span class="number">101</span>], dis[<span class="number">101</span>], book[<span class="number">101</span>], i, j, m, n, a, b, c;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 输入顶点数 n 和边数 m</span></span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>, &amp;n, &amp;m);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 初始化图的邻接矩阵</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= n; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">1</span>; j &lt;= n; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i == j) e[i][j] = <span class="number">0</span>; <span class="comment">// 顶点到自身的距离为 0</span></span><br><span class="line">            <span class="keyword">else</span> e[i][j] = <span class="number">9999</span>; <span class="comment">// 其他情况设为一个较大的值表示无穷大</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 构建图的邻接矩阵，标记边的权值</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= m; i++) &#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d %d %d&quot;</span>, &amp;a, &amp;b, &amp;c);</span><br><span class="line">        e[a][b] = c; <span class="comment">// 顶点 a 到顶点 b 的距离为 c</span></span><br><span class="line">        book[i] = <span class="number">0</span>; <span class="comment">// 初始化标记数组</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 初始化距离数组，初始状态为从顶点 1 到其他顶点的距离</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= n; i++) dis[i] = e[<span class="number">1</span>][i];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 标记顶点 1 已经确定最短路径</span></span><br><span class="line">    book[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 进行 n-1 次循环，每次确定一个顶点的最短路径</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= n - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        <span class="type">int</span> min = <span class="number">9999</span>, u, v;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 在未确定最短路径的顶点中找到离源点最近的顶点</span></span><br><span class="line">        <span class="keyword">for</span> (j = <span class="number">1</span>; j &lt;= n; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (dis[j] &lt; min &amp;&amp; book[j] == <span class="number">0</span>) &#123;</span><br><span class="line">                min = dis[j];</span><br><span class="line">                u = j;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 标记顶点 u 已经确定最短路径</span></span><br><span class="line">        book[u] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 通过顶点 u 来优化其他顶点的最短路径</span></span><br><span class="line">        <span class="keyword">for</span> (v = <span class="number">1</span>; v &lt;= n; v++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (e[u][v] &lt; <span class="number">9999</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (dis[v] &gt; dis[u] + e[u][v]) dis[v] = dis[u] + e[u][v];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 输出从顶点 1 到各个顶点的最短路径</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= n; i++) <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, dis[i]);</span><br><span class="line"></span><br><span class="line">    getchar();</span><br><span class="line">    getchar(); <span class="comment">// 暂停以等待用户输入，以便查看输出结果</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="Dijkstra邻接表实现"><a href="#Dijkstra邻接表实现" class="headerlink" title="Dijkstra邻接表实现"></a>Dijkstra邻接表实现</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INF 0xfffffff</span></span><br><span class="line"></span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll; </span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">Rode</span>&#123;</span></span><br><span class="line">	<span class="type">int</span> u;</span><br><span class="line">	<span class="type">int</span> value;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Rode</span> *<span class="title">next</span>;</span></span><br><span class="line">&#125;Rode;</span><br><span class="line"></span><br><span class="line">Rode rode[<span class="number">100005</span>];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">min</span><span class="params">(<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> a&gt;b?b:a;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> que[<span class="number">100005</span>],book[<span class="number">100005</span>],dis[<span class="number">100005</span>];</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">memset</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">100000</span>;i++)&#123;</span><br><span class="line">		que[i] = <span class="number">0</span>;</span><br><span class="line">		book[i] = <span class="number">0</span>;</span><br><span class="line">		dis[i] = INF;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">int BFS(int x,int y)&#123;</span></span><br><span class="line"><span class="comment">	int front = 1, rear = 0;</span></span><br><span class="line"><span class="comment">	memset();</span></span><br><span class="line"><span class="comment">	que[++rear] = x;</span></span><br><span class="line"><span class="comment">	book[x] = 1;</span></span><br><span class="line"><span class="comment">	dis[x] = 0;</span></span><br><span class="line"><span class="comment">	while(front&lt;=rear)&#123;</span></span><br><span class="line"><span class="comment">		int m = que[front++];</span></span><br><span class="line"><span class="comment">		Rode *p = rode[m].next;</span></span><br><span class="line"><span class="comment">		while(p!=NULL)&#123;</span></span><br><span class="line"><span class="comment">			if(book[p-&gt;u] == 0)&#123;</span></span><br><span class="line"><span class="comment">				dis[p-&gt;u] = min(dis[front] + p-&gt;value,dis[p-&gt;u]);</span></span><br><span class="line"><span class="comment">				que[++rear] = p-&gt;u;</span></span><br><span class="line"><span class="comment">				book[p-&gt;u] = 1;</span></span><br><span class="line"><span class="comment">			&#125;</span></span><br><span class="line"><span class="comment">			p = p-&gt;next;</span></span><br><span class="line"><span class="comment">		&#125;</span></span><br><span class="line"><span class="comment">	&#125; </span></span><br><span class="line"><span class="comment">	return dis[y];</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="type">int</span> n,m;</span><br><span class="line"><span class="type">int</span> <span class="title function_">Dijkstra</span><span class="params">(<span class="type">int</span> p,<span class="type">int</span> x)</span>&#123;</span><br><span class="line">	<span class="built_in">memset</span>();</span><br><span class="line">	Rode *q = rode[p].next;</span><br><span class="line">	<span class="keyword">while</span>(q!=<span class="literal">NULL</span>)&#123;</span><br><span class="line">		dis[q-&gt;u] = q-&gt;value;</span><br><span class="line">		q = q-&gt;next;</span><br><span class="line">	&#125;</span><br><span class="line">	book[p] = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n<span class="number">-1</span>;i++)&#123;</span><br><span class="line">		<span class="type">int</span> min=INF,u,v;</span><br><span class="line">		<span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=n;j++)&#123;						<span class="comment">//寻找离点1最近的且没有遍历过的点 </span></span><br><span class="line">			<span class="keyword">if</span>(dis[j]&lt;min&amp;&amp;book[j]==<span class="number">0</span>)&#123;</span><br><span class="line">				min=dis[j];</span><br><span class="line">				u=j;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		book[u]=<span class="number">1</span>;</span><br><span class="line"><span class="comment">//从该点出发改写其他点的最短距离 </span></span><br><span class="line">		q = rode[u].next;</span><br><span class="line">		<span class="keyword">while</span>(q!=<span class="literal">NULL</span>)&#123;</span><br><span class="line">			<span class="type">int</span> k = q-&gt;u;</span><br><span class="line">			<span class="keyword">if</span>(dis[k]&gt;dis[u]+q-&gt;value) dis[k]=dis[u]+q-&gt;value;</span><br><span class="line">			q = q-&gt;next;</span><br><span class="line">		&#125;	</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> dis[x];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">cin</span> &gt;&gt; n &gt;&gt; m;</span><br><span class="line">	<span class="type">int</span> p,x,y;</span><br><span class="line">	<span class="built_in">cin</span> &gt;&gt; p &gt;&gt; x &gt;&gt; y;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		rode[i].next = <span class="literal">NULL</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=m;i++)&#123;</span><br><span class="line">		<span class="type">int</span> u,v,t;</span><br><span class="line">		<span class="built_in">cin</span> &gt;&gt; u &gt;&gt; v &gt;&gt; t;</span><br><span class="line">		Rode *q = (Rode*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(Rode)), *qq = (Rode*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(Rode));</span><br><span class="line">		<span class="keyword">if</span>(rode[u].next == <span class="literal">NULL</span>)&#123;</span><br><span class="line">			rode[u].next = q;</span><br><span class="line">			q-&gt;u = v;</span><br><span class="line">			q-&gt;value = t;</span><br><span class="line">			q-&gt;next = <span class="literal">NULL</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			q-&gt;next = rode[u].next;</span><br><span class="line">			rode[u].next = q;</span><br><span class="line">			q-&gt;value = t;</span><br><span class="line">			q-&gt;u = v;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(rode[v].next == <span class="literal">NULL</span>)&#123;</span><br><span class="line">			rode[v].next = qq;</span><br><span class="line">			qq-&gt;u = u;</span><br><span class="line">			qq-&gt;next = <span class="literal">NULL</span>;</span><br><span class="line">			qq-&gt;value = t;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			qq-&gt;next = rode[v].next;</span><br><span class="line">			rode[v].next = qq;</span><br><span class="line">			qq-&gt;u = u;</span><br><span class="line">			qq-&gt;value = t;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	for(int i=1;i&lt;=n;i++)&#123;</span></span><br><span class="line"><span class="comment">		Rode *q = rode[i].next;</span></span><br><span class="line"><span class="comment">		while(q!=NULL)&#123;</span></span><br><span class="line"><span class="comment">			printf(&quot;%d &quot;,q-&gt;u);</span></span><br><span class="line"><span class="comment">			q = q-&gt;next;</span></span><br><span class="line"><span class="comment">		&#125;</span></span><br><span class="line"><span class="comment">		cout &lt;&lt; endl;</span></span><br><span class="line"><span class="comment">	&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">	<span class="type">int</span> x1 = Dijkstra(p,x);</span><br><span class="line">	<span class="type">int</span> x2 = Dijkstra(p,y);</span><br><span class="line">	x1 = x1&gt;x2?x2:x1;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span>(x1 == <span class="number">-1</span> || x2 == <span class="number">-1</span>) &#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="number">-1</span>;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="type">int</span> x3 = Dijkstra(x,y);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,x3+x1);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="Floyd-Warshall"><a href="#Floyd-Warshall" class="headerlink" title="Floyd-Warshall"></a>Floyd-Warshall</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ll long long</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 505</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INF 0x3fffffffffff</span></span><br><span class="line"><span class="type">int</span> m,n,p;</span><br><span class="line">ll book[N][N];</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d %d %d&quot;</span>,&amp;n,&amp;m,&amp;p); <span class="comment">// 输入顶点数 n 和边数 m</span></span><br><span class="line">    <span class="type">int</span> i,j,a,b,k;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 初始化邻接矩阵，将所有距离设为无穷大</span></span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        <span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">            <span class="keyword">if</span> (i==j) book[i][j]=<span class="number">0</span>; <span class="comment">// 顶点到自身的距离为 0</span></span><br><span class="line">            <span class="keyword">else</span> book[i][j]=INF; <span class="comment">// 其他情况设为一个较大的值表示无穷大</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 构建邻接矩阵，标记边的权值</span></span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=m;i++)&#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d %d %d&quot;</span>,&amp;a,&amp;b,&amp;k);</span><br><span class="line">        <span class="comment">// 有可能有两条ab一样但是k不同的边</span></span><br><span class="line">		<span class="keyword">if</span>(book[a][b] == INF ) book[a][b]=k;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span>(k &lt; book[a][b]) book[a][b]=k; <span class="comment">// 顶点 a 到顶点 b 的距离为 k</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 使用 Floyd-Warshall 算法求解所有顶点之间的最短路径</span></span><br><span class="line">    <span class="keyword">for</span>(k=<span class="number">1</span>;k&lt;=n;k++)&#123;</span><br><span class="line">        <span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">            <span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">                <span class="keyword">if</span> (book[i][k]+book[k][j]&lt;book[i][j])&#123;</span><br><span class="line">                    book[i][j]=book[i][k]+book[k][j];</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 输出最短路径矩阵</span></span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=p;i++)&#123;</span><br><span class="line">    	<span class="type">int</span> u,v;</span><br><span class="line">    	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;u,&amp;v);</span><br><span class="line">        <span class="keyword">if</span>(book[u][v] != INF) <span class="built_in">printf</span>(<span class="string">&quot;%lld\n&quot;</span>,book[u][v]); </span><br><span class="line">        <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">&quot;-1\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="网络流"><a href="#网络流" class="headerlink" title="网络流"></a>网络流</h2><h4 id="Dinic-最大流"><a href="#Dinic-最大流" class="headerlink" title="Dinic 最大流"></a>Dinic 最大流</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;queue&gt;</span></span></span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> INF = <span class="number">0x7fffffff</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> maxn = <span class="number">10010</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> maxm = <span class="number">100010</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>&#123;</span></span><br><span class="line">	<span class="type">int</span> from,to,cap,flow;</span><br><span class="line">	Edge(<span class="type">int</span> u,<span class="type">int</span> v,<span class="type">int</span> c,<span class="type">int</span> f):from(u),to(v),cap(c),flow(f) &#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Dinic</span>&#123;</span></span><br><span class="line">	<span class="type">int</span> n,m,s,t;</span><br><span class="line">	<span class="built_in">vector</span>&lt;Edge&gt; edges;</span><br><span class="line">	<span class="built_in">vector</span>&lt;<span class="type">int</span>&gt; G[maxn];</span><br><span class="line">	<span class="type">bool</span> vis[maxn];  <span class="comment">//BFS使用 </span></span><br><span class="line">	<span class="type">int</span> d[maxn]; <span class="comment">//从起点到 i的距离 </span></span><br><span class="line">	<span class="type">int</span> cur[maxn]; <span class="comment">//当前弧下标</span></span><br><span class="line">	</span><br><span class="line">	<span class="type">void</span> <span class="title function_">AddEdge</span><span class="params">(<span class="type">int</span> from,<span class="type">int</span> to,<span class="type">int</span> cap)</span>&#123;</span><br><span class="line">		edges.push_back(Edge(from,to,cap,<span class="number">0</span>));</span><br><span class="line">		edges.push_back(Edge(to,from,<span class="number">0</span>,<span class="number">0</span>));</span><br><span class="line">		<span class="type">int</span> m = edges.size();</span><br><span class="line">		G[from].push_back(m<span class="number">-2</span>);</span><br><span class="line">		G[to].push_back(m<span class="number">-1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="type">bool</span> <span class="title function_">BFS</span><span class="params">()</span>&#123;</span><br><span class="line">		<span class="built_in">memset</span>(vis,<span class="number">0</span>,<span class="keyword">sizeof</span>(vis));</span><br><span class="line">		<span class="built_in">queue</span>&lt;<span class="type">int</span>&gt; Q;</span><br><span class="line">		Q.push(s);</span><br><span class="line">		d[s] = <span class="number">0</span>;</span><br><span class="line">		vis[s] = <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">while</span>(!Q.empty())&#123;</span><br><span class="line">			<span class="type">int</span> x = Q.front();Q.pop();</span><br><span class="line">			<span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>;i &lt; G[x].size();i++)&#123;</span><br><span class="line">				Edge &amp;e = edges[G[x][i]];</span><br><span class="line">				<span class="keyword">if</span>(!vis[e.to] &amp;&amp; e.cap &gt; e.flow)&#123;</span><br><span class="line">					vis[e.to] = <span class="number">1</span>;</span><br><span class="line">					d[e.to] = d[x] + <span class="number">1</span>;</span><br><span class="line">					Q.push(e.to);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> vis[t];</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	ll <span class="title function_">DFS</span><span class="params">(<span class="type">int</span> x,<span class="type">int</span> a)</span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(x == t || a == <span class="number">0</span>) <span class="keyword">return</span> a;</span><br><span class="line">		ll flow = <span class="number">0</span>,f;</span><br><span class="line">		<span class="keyword">for</span>(<span class="type">int</span> &amp;i = cur[x];i &lt; G[x].size();i++)&#123;</span><br><span class="line">			Edge &amp;e = edges[G[x][i]];</span><br><span class="line">			<span class="keyword">if</span>(d[x] + <span class="number">1</span> == d[e.to] &amp;&amp; (f = DFS(e.to,min(a,e.cap-e.flow))) &gt; <span class="number">0</span>)&#123;</span><br><span class="line">				e.flow += f;</span><br><span class="line">				edges[G[x][i]^<span class="number">1</span>].flow -= f;</span><br><span class="line">				flow += f;</span><br><span class="line">				a -= f;</span><br><span class="line">				<span class="keyword">if</span>(a == <span class="number">0</span>) <span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> flow;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	ll <span class="title function_">Maxflow</span><span class="params">(<span class="type">int</span> s,<span class="type">int</span> t)</span>&#123;</span><br><span class="line">		this-&gt;s = s;this-&gt;t = t;</span><br><span class="line">		ll flow = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">while</span>(BFS())&#123;</span><br><span class="line">			<span class="built_in">memset</span>(cur,<span class="number">0</span>,<span class="keyword">sizeof</span>(cur));</span><br><span class="line">			flow += DFS(s,INF);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> flow;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> n,m,s,t,u,v,w;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Dinic</span> <span class="title">dn</span>;</span></span><br><span class="line">	<span class="built_in">cin</span> &gt;&gt; n &gt;&gt; m &gt;&gt; s &gt;&gt; t;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>;i &lt; m;i++)&#123;</span><br><span class="line">		<span class="built_in">cin</span> &gt;&gt; u &gt;&gt; v &gt;&gt; w;</span><br><span class="line">		dn.AddEdge(u,v,w);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; dn.Maxflow(s,t);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="多源点，多汇点"><a href="#多源点，多汇点" class="headerlink" title="多源点，多汇点"></a>多源点，多汇点</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">A 国经过长时间的排查，摸清楚了国内活动的他国间谍的信息，并准备开展一次行动将他们一网打尽。</span><br><span class="line"></span><br><span class="line">可是，行动计划在前一天晚上被内鬼泄露出去了，间谍们打算连夜转移到一些安全屋内，来躲避天亮开始的抓捕行动。已知有n个间谍和n个安全屋，一个安全屋只能容纳一个间谍，间谍和安全屋均有一个二维坐标。在天亮之前，一个间谍只能移动至多d的距离（指欧氏距离）。</span><br><span class="line"></span><br><span class="line">已知只有躲进安全屋中的间谍可以避免被抓获，且间谍们非常团结，他们会尽可能让最多的人避免被抓。请你计算一下，至多有多少个间谍不会被抓获呢？</span><br><span class="line"></span><br><span class="line">输入格式</span><br><span class="line">第一行两个整数，为n和d。</span><br><span class="line"></span><br><span class="line">接下来n行，每行两个整数x,y，表示一个间谍的坐标。</span><br><span class="line"></span><br><span class="line">接下来n行，每行两个整数x,y，表示一个安全屋的坐标。</span><br><span class="line"></span><br><span class="line">输出格式</span><br><span class="line">输出一个正整数，表示最多有几个间谍可以避免被抓获。</span><br></pre></td></tr></table></figure>

<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br></pre></td><td class="code"><pre><span class="line">##解题思路</span><br><span class="line"></span><br><span class="line">##AC代码</span><br><span class="line">```c++</span><br><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> Author: 林智灏</span></span><br><span class="line"><span class="comment"> Result: AC	Submission_id: 3850740</span></span><br><span class="line"><span class="comment"> Created at: Mon Nov 22 2021 20:33:09 GMT+0800 (China Standard Time)</span></span><br><span class="line"><span class="comment"> Problem_id: 5095	Time: 32	Memory: 6560</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">rank</span><span class="params">(<span class="type">int</span> matrix[<span class="number">205</span>][<span class="number">205</span>],<span class="type">int</span> r,<span class="type">int</span> c)</span></span>;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">standard_echelon</span><span class="params">(<span class="type">int</span> matrix[<span class="number">205</span>][<span class="number">205</span>],<span class="type">int</span> r,<span class="type">int</span> c,<span class="type">int</span> x,<span class="type">int</span> y)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> INF = <span class="number">0x7fffffff</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> maxn = <span class="number">10010</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> maxm = <span class="number">100010</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Edge</span>&#123;</span><br><span class="line">	<span class="type">int</span> from,to,cap,flow;</span><br><span class="line">	<span class="built_in">Edge</span>(<span class="type">int</span> u,<span class="type">int</span> v,<span class="type">int</span> c,<span class="type">int</span> f):<span class="built_in">from</span>(u),<span class="built_in">to</span>(v),<span class="built_in">cap</span>(c),<span class="built_in">flow</span>(f) &#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Dinic</span>&#123;</span><br><span class="line">	<span class="type">int</span> n,m,s,t;</span><br><span class="line">	vector&lt;Edge&gt; edges;</span><br><span class="line">	vector&lt;<span class="type">int</span>&gt; G[maxn];</span><br><span class="line">	<span class="type">bool</span> vis[maxn];  <span class="comment">//BFS使用 </span></span><br><span class="line">	<span class="type">int</span> d[maxn]; <span class="comment">//从起点到 i的距离 </span></span><br><span class="line">	<span class="type">int</span> cur[maxn]; <span class="comment">//当前弧下标</span></span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">AddEdge</span><span class="params">(<span class="type">int</span> from,<span class="type">int</span> to,<span class="type">int</span> cap)</span></span>&#123;</span><br><span class="line">		edges.<span class="built_in">push_back</span>(<span class="built_in">Edge</span>(from,to,cap,<span class="number">0</span>));</span><br><span class="line">		edges.<span class="built_in">push_back</span>(<span class="built_in">Edge</span>(to,from,<span class="number">0</span>,<span class="number">0</span>));</span><br><span class="line">		<span class="type">int</span> m = edges.<span class="built_in">size</span>();</span><br><span class="line">		G[from].<span class="built_in">push_back</span>(m<span class="number">-2</span>);</span><br><span class="line">		G[to].<span class="built_in">push_back</span>(m<span class="number">-1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="type">bool</span> <span class="title">BFS</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="built_in">memset</span>(vis,<span class="number">0</span>,<span class="built_in">sizeof</span>(vis));</span><br><span class="line">		queue&lt;<span class="type">int</span>&gt; Q;</span><br><span class="line">		Q.<span class="built_in">push</span>(s);</span><br><span class="line">		d[s] = <span class="number">0</span>;</span><br><span class="line">		vis[s] = <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">while</span>(!Q.<span class="built_in">empty</span>())&#123;</span><br><span class="line">			<span class="type">int</span> x = Q.<span class="built_in">front</span>();Q.<span class="built_in">pop</span>();</span><br><span class="line">			<span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>;i &lt; G[x].<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">				Edge &amp;e = edges[G[x][i]];</span><br><span class="line">				<span class="keyword">if</span>(!vis[e.to] &amp;&amp; e.cap &gt; e.flow)&#123;</span><br><span class="line">					vis[e.to] = <span class="number">1</span>;</span><br><span class="line">					d[e.to] = d[x] + <span class="number">1</span>;</span><br><span class="line">					Q.<span class="built_in">push</span>(e.to);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> vis[t];</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function">ll <span class="title">DFS</span><span class="params">(<span class="type">int</span> x,<span class="type">int</span> a)</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(x == t || a == <span class="number">0</span>) <span class="keyword">return</span> a;</span><br><span class="line">		ll flow = <span class="number">0</span>,f;</span><br><span class="line">		<span class="keyword">for</span>(<span class="type">int</span> &amp;i = cur[x];i &lt; G[x].<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">			Edge &amp;e = edges[G[x][i]];</span><br><span class="line">			<span class="keyword">if</span>(d[x] + <span class="number">1</span> == d[e.to] &amp;&amp; (f = <span class="built_in">DFS</span>(e.to,<span class="built_in">min</span>(a,e.cap-e.flow))) &gt; <span class="number">0</span>)&#123;</span><br><span class="line">				e.flow += f;</span><br><span class="line">				edges[G[x][i]^<span class="number">1</span>].flow -= f;</span><br><span class="line">				flow += f;</span><br><span class="line">				a -= f;</span><br><span class="line">				<span class="keyword">if</span>(a == <span class="number">0</span>) <span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> flow;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function">ll <span class="title">Maxflow</span><span class="params">(<span class="type">int</span> s,<span class="type">int</span> t)</span></span>&#123;</span><br><span class="line">		<span class="keyword">this</span>-&gt;s = s;<span class="keyword">this</span>-&gt;t = t;</span><br><span class="line">		ll flow = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">while</span>(<span class="built_in">BFS</span>())&#123;</span><br><span class="line">			<span class="built_in">memset</span>(cur,<span class="number">0</span>,<span class="built_in">sizeof</span>(cur));</span><br><span class="line">			flow += <span class="built_in">DFS</span>(s,INF);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> flow;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">struct</span>&#123;</span><br><span class="line">	<span class="type">int</span> x;</span><br><span class="line">	<span class="type">int</span> y;	</span><br><span class="line">&#125;Position;</span><br><span class="line"></span><br><span class="line">Position jd[<span class="number">205</span>],house[<span class="number">205</span>];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="type">int</span> n,d;</span><br><span class="line">	cin &gt;&gt; n &gt;&gt; d;</span><br><span class="line">	<span class="type">double</span> e[<span class="number">205</span>][<span class="number">205</span>];</span><br><span class="line">	<span class="type">int</span> m[<span class="number">205</span>][<span class="number">205</span>];</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		cin &gt;&gt; jd[i].x &gt;&gt; jd[i].y;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		cin &gt;&gt; house[i].x &gt;&gt; house[i].y;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">			<span class="type">double</span> a = <span class="built_in">pow</span>((jd[i].x-house[j].x)*(jd[i].x-house[j].x)+(jd[i].y-house[j].y)*(jd[i].y-house[j].y),<span class="number">0.5</span>);</span><br><span class="line">			e[i][j] = a;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(e[i][j] &gt; d) m[i][j] = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">else</span> m[i][j] = <span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">Dinic</span> dn;</span><br><span class="line">	<span class="type">int</span> s = <span class="number">0</span>,t = <span class="number">2</span>*n+<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		dn.<span class="built_in">AddEdge</span>(<span class="number">0</span>,i,<span class="number">1</span>);</span><br><span class="line">		dn.<span class="built_in">AddEdge</span>(n+i,<span class="number">2</span>*n+<span class="number">1</span>,<span class="number">1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">1</span>;i &lt;=n ;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(m[i][j] == <span class="number">1</span>)</span><br><span class="line">				dn.<span class="built_in">AddEdge</span>(i,j+n,<span class="number">1</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; dn.<span class="built_in">Maxflow</span>(s,t);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line">```</span><br></pre></td></tr></table></figure>



<h3 id="树的相遇"><a href="#树的相遇" class="headerlink" title="树的相遇"></a>树的相遇</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果t是u，v的最近公共祖先，那么d[u,v]=d[u,root]+d[v,root]-2*d[t,root]</span></span><br><span class="line"></span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="type">int</span>&gt; E[<span class="number">100005</span>];</span><br><span class="line"><span class="type">int</span> Queue[<span class="number">100005</span>];</span><br><span class="line"><span class="type">int</span> book[<span class="number">100005</span>];</span><br><span class="line"><span class="type">int</span> dis[<span class="number">100005</span>];</span><br><span class="line"><span class="type">int</span> f[<span class="number">100005</span>];</span><br><span class="line"><span class="type">int</span> n,m;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">BFS</span><span class="params">(<span class="type">int</span> a)</span> &#123; <span class="comment">// 从点a开始搜索 </span></span><br><span class="line">	</span><br><span class="line">	<span class="type">int</span> i,head=<span class="number">1</span>,rear=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;=<span class="number">10000</span>;i++) &#123;</span><br><span class="line">		dis[i] = <span class="number">0</span>;</span><br><span class="line">		book[i] = <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	Queue[++rear] = a;</span><br><span class="line">	book[a] = <span class="number">1</span>;</span><br><span class="line">	dis[a] = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(head&lt;=rear)&#123;</span><br><span class="line">		<span class="type">int</span> m = Queue[head++];</span><br><span class="line">		<span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">0</span>;j&lt;E[m].size();j++)&#123;</span><br><span class="line">			<span class="type">int</span> i = E[m][j];</span><br><span class="line">			<span class="keyword">if</span>(book[i] == <span class="number">0</span> ) &#123;</span><br><span class="line">				book[i] = <span class="number">1</span>;</span><br><span class="line">				Queue[++rear] = i;</span><br><span class="line">                <span class="comment">// 这里直接用相邻的分别为0，1表示，简化了距离的表示</span></span><br><span class="line">				dis[i] = <span class="number">1</span> - dis[m];</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;	</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n<span class="number">-1</span>;i++)&#123; </span><br><span class="line">		<span class="type">int</span> a,b;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;a,&amp;b);</span><br><span class="line">		E[a].push_back(b);</span><br><span class="line">		E[b].push_back(a);</span><br><span class="line">	&#125;</span><br><span class="line">	BFS(<span class="number">1</span>);</span><br><span class="line">	<span class="type">int</span> q;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;q);</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=q;i++)&#123;</span><br><span class="line">		<span class="type">int</span> a,b;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;a,&amp;b);</span><br><span class="line">		<span class="keyword">if</span>(dis[a] == dis[b]) <span class="built_in">printf</span>(<span class="string">&quot;YE5\n&quot;</span>);</span><br><span class="line">		<span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">&quot;N0\n&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h2><h3 id="最大边的最小值"><a href="#最大边的最小值" class="headerlink" title="最大边的最小值"></a>最大边的最小值</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ins 星系中有两颗适宜人类居住的星球。</span><br><span class="line"></span><br><span class="line">这次的星际移民中包含了n个不同的部落，他们会分散居住在两颗星球上。这些部落之间可能存在仇怨，如果存在仇怨的两个部落居住在一个星球上那么他们之间就会发生部落冲突。部落冲突可大可小，严重冲突是指所有部落冲突中最大的那一个。</span><br><span class="line"></span><br><span class="line">如何安排部落的居住方案才能使得部落间发生的严重冲突尽可能的小呢？</span><br></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ll long long</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> M 100005</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 20005</span></span><br><span class="line"><span class="comment">// 定义Edge结构，表示一条有向边</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> &#123;</span></span><br><span class="line">    <span class="type">int</span> y;</span><br><span class="line">    <span class="type">int</span> w;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>* <span class="title">next</span>;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>* <span class="title">G</span>[<span class="title">N</span>];</span>  <span class="comment">// G表示图，每个节点的头指针初始化为NULL</span></span><br><span class="line"><span class="type">int</span> p[N];  <span class="comment">// 用于染色判断是否冲突的数组</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数Max用于判断在给定权值w下，图中是否存在冲突</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Max</span><span class="params">(<span class="type">int</span> w, <span class="type">int</span> n, <span class="type">int</span> m)</span> &#123;</span><br><span class="line">    <span class="built_in">memset</span>(p, <span class="number">-1</span>, <span class="keyword">sizeof</span>(p));</span><br><span class="line">    <span class="type">int</span> Q[N], front = <span class="number">0</span>, rear = <span class="number">0</span>;  <span class="comment">// 使用数组实现简单队列</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (p[i] == <span class="number">-1</span>) &#123;</span><br><span class="line">            p[i] = <span class="number">0</span>;</span><br><span class="line">            Q[rear++] = i;</span><br><span class="line">            <span class="keyword">while</span> (front != rear) &#123;</span><br><span class="line">                <span class="type">int</span> x = Q[front++];</span><br><span class="line">                <span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>* <span class="title">e</span> =</span> G[x];</span><br><span class="line">                <span class="keyword">while</span> (e != <span class="literal">NULL</span>) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (e-&gt;w &gt; w) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (p[e-&gt;y] == <span class="number">-1</span>) &#123;</span><br><span class="line">                            p[e-&gt;y] = <span class="number">1</span> - p[x];</span><br><span class="line">                            Q[rear++] = e-&gt;y;</span><br><span class="line">                        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (p[e-&gt;y] == p[x]) &#123;</span><br><span class="line">                            <span class="keyword">return</span> <span class="number">1</span>;  <span class="comment">// 存在冲突</span></span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                    e = e-&gt;next;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">addEdge</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y, <span class="type">int</span> w)</span> &#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>* <span class="title">newEdge</span> =</span> (<span class="keyword">struct</span> Edge*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Edge));</span><br><span class="line">    newEdge-&gt;y = y;</span><br><span class="line">    newEdge-&gt;w = w;</span><br><span class="line">    newEdge-&gt;next = G[x];</span><br><span class="line">    G[x] = newEdge;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">int</span> n, m, x, y, w, mid, l, r;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>, &amp;n, &amp;m);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; m; i++) &#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d %d %d&quot;</span>, &amp;x, &amp;y, &amp;w);</span><br><span class="line">        addEdge(x, y, w);  <span class="comment">// 添加边x-&gt;y</span></span><br><span class="line">        addEdge(y, x, w);  <span class="comment">// 添加边y-&gt;x</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    l = <span class="number">-1</span>;</span><br><span class="line">    r = <span class="number">1e9</span>;</span><br><span class="line">    <span class="keyword">while</span> (l &lt; r) &#123;</span><br><span class="line">        mid = (l + r + <span class="number">2</span>) / <span class="number">2</span> - <span class="number">1</span>;</span><br><span class="line">        <span class="type">int</span> a = Max(mid, n, m);</span><br><span class="line">        <span class="keyword">if</span> (a) &#123;</span><br><span class="line">            l = mid + <span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            r = mid;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (l == <span class="number">-1</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Peace And Love!\n&quot;</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, l);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 清理分配的内存</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</span><br><span class="line">        <span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>* <span class="title">e</span> =</span> G[i];</span><br><span class="line">        <span class="keyword">while</span> (e != <span class="literal">NULL</span>) &#123;</span><br><span class="line">            <span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>* <span class="title">tmp</span> =</span> e;</span><br><span class="line">            e = e-&gt;next;</span><br><span class="line">            <span class="built_in">free</span>(tmp);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h1 id="计算几何"><a href="#计算几何" class="headerlink" title="计算几何"></a>计算几何</h1><h3 id="判断两条直线是否相交"><a href="#判断两条直线是否相交" class="headerlink" title="判断两条直线是否相交"></a>判断两条直线是否相交</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span>&#123;</span></span><br><span class="line">	<span class="type">int</span> x;</span><br><span class="line">	<span class="type">int</span> y;</span><br><span class="line">&#125;Point;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">DIRECTION</span><span class="params">(Point p1, Point p2, Point p3)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> (p3.x-p1.x)*(p2.y-p1.y) - (p2.x-p1.x)*(p3.y-p1.y);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">max</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> a&gt;b?a:b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">min</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> a&lt;b?a:b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">ON_SEGMENT</span><span class="params">(Point p1, Point p2, Point p3)</span>&#123;</span><br><span class="line">	<span class="keyword">if</span>((p3.x&gt;=min(p1.x,p2.x)&amp;&amp;p3.x&lt;=max(p1.x,p2.x))&amp;&amp;(p3.y&gt;=min(p1.y,p2.y)&amp;&amp;p3.y&lt;=max(p1.y,p2.y))) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">SEGMENTS_INTERSECT</span><span class="params">(Point p1, Point p2, Point p3, Point p4)</span>&#123;</span><br><span class="line">	<span class="type">int</span> d1 = DIRECTION(p3, p4, p1);</span><br><span class="line">	<span class="type">int</span> d2 = DIRECTION(p3, p4, p2);</span><br><span class="line">	<span class="type">int</span> d3 = DIRECTION(p1, p2, p3);</span><br><span class="line">	<span class="type">int</span> d4 = DIRECTION(p1, p2, p4);</span><br><span class="line">	<span class="keyword">if</span>(d1*d2 &lt; <span class="number">0</span> &amp;&amp; d3*d4 &lt; <span class="number">0</span>)&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> ((d1 == <span class="number">0</span>) &amp;&amp; ON_SEGMENT(p3, p4, p1))&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> ((d2 == <span class="number">0</span>) &amp;&amp; ON_SEGMENT(p3, p4, p2))&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> ((d3 == <span class="number">0</span>) &amp;&amp; ON_SEGMENT(p1, p2, p3))&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> ((d4 == <span class="number">0</span>) &amp;&amp; ON_SEGMENT(p1, p2, p4))&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	Point p1,p2,p3,p4; </span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;p1.x,&amp;p1.y);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;p2.x,&amp;p2.y);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;p3.x,&amp;p3.y);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;p4.x,&amp;p4.y);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,SEGMENTS_INTERSECT(p1,p2,p3,p4));</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="多边形面积"><a href="#多边形面积" class="headerlink" title="多边形面积"></a>多边形面积</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用typedef定义长整型的别名</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义点的结构</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">Point</span> &#123;</span></span><br><span class="line">    <span class="type">int</span> x, y;</span><br><span class="line">&#125; Point;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 重载操作符&quot;-&quot;的C语言版本函数</span></span><br><span class="line">Point <span class="title function_">subtract</span><span class="params">(Point a, Point b)</span> &#123;</span><br><span class="line">    Point result;</span><br><span class="line">    result.x = a.x - b.x;</span><br><span class="line">    result.y = a.y - b.y;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 重载操作符&quot;^&quot;的C语言版本函数（叉积）</span></span><br><span class="line"><span class="type">double</span> <span class="title function_">crossProduct</span><span class="params">(Point a, Point b)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> a.x * b.y - a.y * b.x;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 重载操作符&quot;*&quot;的C语言版本函数（点积）</span></span><br><span class="line"><span class="type">double</span> <span class="title function_">dotProduct</span><span class="params">(Point a, Point b)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> a.x * b.x + a.y * b.y;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 绕原点旋转角度B（弧度值）后x,y的变化的C语言版本函数</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">transXY</span><span class="params">(Point *p, <span class="type">double</span> B)</span> &#123;</span><br><span class="line">    <span class="type">double</span> tx = p-&gt;x, ty = p-&gt;y;</span><br><span class="line">    p-&gt;x = tx * <span class="built_in">cos</span>(B) - ty * <span class="built_in">sin</span>(B);</span><br><span class="line">    p-&gt;y = tx * <span class="built_in">sin</span>(B) + ty * <span class="built_in">cos</span>(B);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义点数组</span></span><br><span class="line">Point point[<span class="number">305</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 计算多边形面积，点的编号从0到n-1</span></span><br><span class="line"><span class="type">double</span> <span class="title function_">CalcArea</span><span class="params">(<span class="type">int</span> n)</span> &#123;</span><br><span class="line">    <span class="type">double</span> res = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">        Point temp = subtract(point[i], point[(i + <span class="number">1</span>) % n]);  <span class="comment">// 使用subtract函数计算两点之差</span></span><br><span class="line">        <span class="type">double</span> a = crossProduct(temp, point[(i + <span class="number">1</span>) % n]) / <span class="number">2</span>;  <span class="comment">// 使用crossProduct函数计算叉积</span></span><br><span class="line">        res += a;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">fabs</span>(res);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">int</span> n;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;n);</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>, &amp;point[i].x, &amp;point[i].y);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%.0lf&quot;</span>, CalcArea(n));</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h3 id="n个点-求凸包周长"><a href="#n个点-求凸包周长" class="headerlink" title="n个点 求凸包周长"></a>n个点 求凸包周长</h3><h3 id="Graham寻找凸包（错误）"><a href="#Graham寻找凸包（错误）" class="headerlink" title="Graham寻找凸包（错误）"></a>Graham寻找凸包（错误）</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span>&#123;</span></span><br><span class="line">	<span class="type">int</span> x;</span><br><span class="line">	<span class="type">int</span> y;</span><br><span class="line">&#125;Point;</span><br><span class="line"></span><br><span class="line">Point point[<span class="number">100005</span>]; </span><br><span class="line">Point <span class="built_in">stack</span>[<span class="number">100005</span>];</span><br><span class="line"><span class="type">int</span> top = <span class="number">2</span>,len = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">cmp</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *a, <span class="type">const</span> <span class="type">void</span>*b)</span>&#123; </span><br><span class="line">	Point p = *(Point*)a; </span><br><span class="line">	Point q = *(Point*)b; </span><br><span class="line">	<span class="keyword">if</span>(p.y != q.y) <span class="keyword">return</span> p.y - q.y; </span><br><span class="line">	<span class="keyword">return</span> p.x - q.x;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">multiply</span><span class="params">(Point p1, Point p2, Point p0)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">double</span> <span class="title function_">distance</span><span class="params">(Point p1, Point p2)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">pow</span>(((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)),<span class="number">0.5</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">GRAHAM_SCAN</span><span class="params">(<span class="type">int</span> n)</span>&#123;</span><br><span class="line">	qsort(point,n,<span class="keyword">sizeof</span>(Point),cmp);</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;n;i++)&#123;</span><br><span class="line">		<span class="type">int</span> j,k=i;</span><br><span class="line">		<span class="keyword">for</span>(j=i+<span class="number">1</span>;j&lt;n;j++)&#123;</span><br><span class="line">			<span class="keyword">if</span>((multiply(point[j],point[k],point[<span class="number">0</span>])&gt;<span class="number">0</span>) || (multiply(point[j],point[k],point[<span class="number">0</span>])==<span class="number">0</span> &amp;&amp; distance(point[<span class="number">0</span>],point[j])&lt;distance(point[<span class="number">0</span>],point[k]))) k=j;</span><br><span class="line">		&#125;</span><br><span class="line">		Point tmp = point[i];</span><br><span class="line">		point[i] = point[k];</span><br><span class="line">		point[k] = tmp;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">stack</span>[<span class="number">0</span>] = point[<span class="number">0</span>];</span><br><span class="line">	<span class="built_in">stack</span>[<span class="number">1</span>] = point[<span class="number">1</span>];</span><br><span class="line">	<span class="built_in">stack</span>[<span class="number">2</span>] = point[<span class="number">2</span>];</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">3</span>;i&lt;n;i++)&#123;</span><br><span class="line">		<span class="keyword">while</span>(multiply(point[i],<span class="built_in">stack</span>[top],<span class="built_in">stack</span>[top<span class="number">-1</span>])&gt;=<span class="number">0</span>) top--;</span><br><span class="line">		<span class="built_in">stack</span>[++top] = point[i];</span><br><span class="line">	&#125;</span><br><span class="line">	len = top+<span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> n,i;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;point[i].x,&amp;point[i].y);</span><br><span class="line">	&#125;</span><br><span class="line">	GRAHAM_SCAN(n);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,len);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;len;i++)&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%d %d\n&quot;</span>,<span class="built_in">stack</span>[i].x,<span class="built_in">stack</span>[i].y);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="正确的"><a href="#正确的" class="headerlink" title="正确的"></a>正确的</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">point</span>&#123;</span></span><br><span class="line">    <span class="type">double</span> x,y;</span><br><span class="line">    point friend operator -(point a,point b)</span><br><span class="line">    &#123;<span class="keyword">return</span> &#123;a.x-b.x,a.y-b.y&#125;;&#125;</span><br><span class="line">&#125;p[<span class="number">200005</span>],s[<span class="number">200005</span>];</span><br><span class="line"><span class="type">double</span> <span class="title function_">dis</span><span class="params">(point a,point b)</span></span><br><span class="line">&#123;</span><br><span class="line">    point c=a-b;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">sqrt</span>(c.x*c.x+c.y*c.y);</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">double</span> <span class="title function_">X</span><span class="params">(point a,point b)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> a.x*b.y-a.y*b.x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">cmp</span><span class="params">(point a,point b)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">double</span> x=X(a-p[<span class="number">1</span>],b-p[<span class="number">1</span>]);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span>(x&gt;<span class="number">0</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span>(x==<span class="number">0</span>&amp;&amp;dis(a,p[<span class="number">1</span>])&lt;dis(b,p[<span class="number">1</span>])) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">double</span> <span class="title function_">multi</span><span class="params">(point p1,point p2,point p3)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> X(p2-p1,p3-p1);</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> N;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;N);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=N;i++) <span class="built_in">cin</span>&gt;&gt;p[i].x&gt;&gt;p[i].y;</span><br><span class="line">        </span><br><span class="line">   </span><br><span class="line">    <span class="type">int</span> k=<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">2</span>;i&lt;=N;i++)</span><br><span class="line">    	<span class="keyword">if</span>(p[i].y&lt;p[k].y||(p[i].y==p[k].y&amp;&amp;p[i].x&lt;p[k].x))k=i;</span><br><span class="line">    swap(p[<span class="number">1</span>],p[k]);</span><br><span class="line">        </span><br><span class="line">    sort(p+<span class="number">2</span>,p+<span class="number">1</span>+N,cmp);</span><br><span class="line">        </span><br><span class="line">    s[<span class="number">1</span>]=p[<span class="number">1</span>];</span><br><span class="line">    s[<span class="number">2</span>]=p[<span class="number">2</span>];</span><br><span class="line">    <span class="type">int</span> t=<span class="number">2</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">3</span>;i&lt;=N;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">while</span>(t&gt;=<span class="number">2</span>&amp;&amp;multi(s[t<span class="number">-1</span>],s[t],p[i])&lt;=<span class="number">0</span>) t--;</span><br><span class="line">        s[++t]=p[i];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="type">double</span> sum=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;t;i++)</span><br><span class="line">    &#123;</span><br><span class="line">          rintf(<span class="string">&quot;%.2lf\n&quot;</span>,sum+dis(s[<span class="number">1</span>],s[t]));</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;补充</span><br></pre></td></tr></table></figure>

<h5 id="计算凸包面积"><a href="#计算凸包面积" class="headerlink" title="计算凸包面积"></a>计算凸包面积</h5><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//计算多边形面积</span></span><br><span class="line"><span class="comment">//点的编号从0~n-1</span></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">CalcArea</span><span class="params">(Point p[], <span class="type">int</span> n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">double</span> res = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">		res += (p[i] ^ p[(i + <span class="number">1</span>) % n]) / <span class="number">2</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">fabs</span>(res);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="判断点在线段上"><a href="#判断点在线段上" class="headerlink" title="判断点在线段上"></a>判断点在线段上</h5><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//*判断点在线段上</span></span><br><span class="line"><span class="function"><span class="type">bool</span> <span class="title">OnSeg</span><span class="params">(Point P, Line L)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span></span><br><span class="line">		<span class="built_in">sgn</span>((L.s - P) ^ (L.e - P)) == <span class="number">0</span> &amp;&amp;</span><br><span class="line">		<span class="built_in">sgn</span>((P.x - L.s.x) * (P.x - L.e.x)) &lt;= <span class="number">0</span> &amp;&amp;</span><br><span class="line">		<span class="built_in">sgn</span>((P.y - L.s.y) * (P.y - L.e.y)) &lt;= <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="判断点在凸包内"><a href="#判断点在凸包内" class="headerlink" title="判断点在凸包内"></a>判断点在凸包内</h5><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//*判断点在凸多边形内</span></span><br><span class="line"><span class="comment">//点形成一个凸包，而且按逆时针排序（如果是顺时针把里面的&lt;0改为&gt;0）</span></span><br><span class="line"><span class="comment">//点的编号:0~n-1</span></span><br><span class="line"><span class="comment">//返回值：</span></span><br><span class="line"><span class="comment">//-1:点在凸多边形外</span></span><br><span class="line"><span class="comment">//0:点在凸多边形边界上</span></span><br><span class="line"><span class="comment">//1:点在凸多边形内</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">inConvexPoly</span><span class="params">(Point a, Point p[], <span class="type">int</span> n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (<span class="built_in">sgn</span>((p[i] - a) ^ (p[(i + <span class="number">1</span>) % n] - a)) &lt; <span class="number">0</span>)<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">OnSeg</span>(a, <span class="built_in">Line</span>(p[i], p[(i + <span class="number">1</span>) % n])))<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="Jarvis步进法寻找凸包"><a href="#Jarvis步进法寻找凸包" class="headerlink" title="Jarvis步进法寻找凸包"></a>Jarvis步进法寻找凸包</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">POINT</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">int</span> x,y;</span><br><span class="line">&#125;;</span><br><span class="line">POINT point[<span class="number">100</span>],pk;</span><br><span class="line"><span class="type">int</span> n,top,k,Stack[<span class="number">100</span>];</span><br><span class="line"> </span><br><span class="line"><span class="type">int</span> <span class="title function_">det</span><span class="params">(POINT a,POINT b,POINT c)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="type">int</span> <span class="title function_">dis</span><span class="params">(POINT a,POINT b)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="type">void</span> <span class="title function_">Jarvis</span><span class="params">(<span class="type">int</span> n,<span class="type">int</span> flag)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> m=<span class="number">0</span>,tmp;</span><br><span class="line">	POINT pm;</span><br><span class="line">	Stack[<span class="number">0</span>]=<span class="number">0</span>;<span class="comment">//p0进栈</span></span><br><span class="line">	top=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(m!=k)</span><br><span class="line">	&#123;</span><br><span class="line">		pm=pk;m=k;</span><br><span class="line">		<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;n;i++)</span><br><span class="line">		&#123;</span><br><span class="line">			tmp=det(point[Stack[top]],point[i],pm);</span><br><span class="line">			<span class="keyword">if</span>((tmp&gt;<span class="number">0</span>&amp;&amp;flag==<span class="number">1</span>)||(tmp&lt;<span class="number">0</span>&amp;&amp;flag==<span class="number">0</span>)||</span><br><span class="line">			    (tmp==<span class="number">0</span>)&amp;&amp;(dis(point[Stack[top]],point[i])&gt;dis(point[Stack[top]],pm)))</span><br><span class="line">				&#123;</span><br><span class="line">					pm=point[i];</span><br><span class="line">					m=i;</span><br><span class="line">				&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		top++;</span><br><span class="line">		Stack[top]=m;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(flag==<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">	    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;=top;i++)</span><br><span class="line">		    <span class="built_in">printf</span>(<span class="string">&quot;(%d,%d)&quot;</span>,point[Stack[i]].x,point[Stack[i]].y);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(flag==<span class="number">0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">	    <span class="keyword">for</span>(<span class="type">int</span> i=top<span class="number">-1</span>;i&gt;<span class="number">0</span>;i--)</span><br><span class="line">		    <span class="built_in">printf</span>(<span class="string">&quot;(%d,%d)&quot;</span>,point[Stack[i]].x,point[Stack[i]].y);</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">	&#125;		</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;n;i++)</span><br><span class="line">	&#123;</span><br><span class="line">	    <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;(point[i].x),&amp;(point[i].y));</span><br><span class="line">		<span class="keyword">if</span>(point[i].y&lt;point[<span class="number">0</span>].y||point[i].y==point[<span class="number">0</span>].y&amp;&amp;point[i].x&lt;point[<span class="number">0</span>].x)<span class="comment">//最左最低点 </span></span><br><span class="line">		    swap(point[<span class="number">0</span>],point[i]);</span><br><span class="line">		<span class="keyword">if</span>(i==<span class="number">0</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			pk=point[<span class="number">0</span>];</span><br><span class="line">			k=<span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(point[i].y&gt;pk.y||point[i].y==pk.y&amp;&amp;point[i].x&gt;pk.x)<span class="comment">//最右最高点 </span></span><br><span class="line">		&#123;</span><br><span class="line">			pk=point[i];</span><br><span class="line">			k=i;</span><br><span class="line">		&#125; </span><br><span class="line">	&#125;</span><br><span class="line">	Jarvis(n,<span class="number">1</span>);<span class="comment">//右链 </span></span><br><span class="line">	Jarvis(n,<span class="number">0</span>);<span class="comment">//左链 </span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="求两条线段交点"><a href="#求两条线段交点" class="headerlink" title="求两条线段交点"></a>求两条线段交点</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">double</span> eps=<span class="number">1e-8</span>,pi=<span class="built_in">acos</span>(<span class="number">-1.0</span>);</span><br><span class="line"><span class="type">int</span> n;</span><br><span class="line"><span class="type">int</span> <span class="title function_">dcmp</span><span class="params">(<span class="type">double</span> x)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">fabs</span>(x)&lt;eps)<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">return</span> x&gt;<span class="number">0</span>?<span class="number">1</span>:<span class="number">-1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span>&#123;</span></span><br><span class="line">    <span class="type">double</span> x,y;</span><br><span class="line">    <span class="keyword">inline</span> <span class="title function_">Point</span><span class="params">(<span class="type">double</span> x=<span class="number">0</span>,<span class="type">double</span> y=<span class="number">0</span>)</span>:<span class="title function_">x</span><span class="params">(x)</span>,<span class="title function_">y</span><span class="params">(y)</span>&#123;&#125;</span><br><span class="line">&#125;line1[<span class="number">5</span>],line2[<span class="number">5</span>];</span><br><span class="line"><span class="type">bool</span> <span class="title function_">myCmp</span><span class="params">(Point A,Point B)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(A.x!=B.x)<span class="keyword">return</span> A.x&lt;B.x;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">return</span> A.y&lt;B.y;</span><br><span class="line">&#125;</span><br><span class="line">Point operator + (Point A,Point B)&#123;</span><br><span class="line">    <span class="keyword">return</span> Point(A.x+B.x,A.y+B.y);</span><br><span class="line">&#125;</span><br><span class="line">Point operator - (Point A,Point B)&#123;</span><br><span class="line">    <span class="keyword">return</span> Point(A.x-B.x,A.y-B.y);</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">double</span> operator ^ (Point A,Point B)&#123;</span><br><span class="line">    <span class="keyword">return</span> A.x*B.y-A.y*B.x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">bool</span> operator == (<span class="type">const</span> Point &amp;A,<span class="type">const</span> Point &amp;B)&#123;</span><br><span class="line">    <span class="keyword">return</span> dcmp(A.x-B.x)==<span class="number">0</span>&amp;&amp;dcmp(A.y-B.y)==<span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">check</span><span class="params">()</span>&#123;<span class="comment">//线段相交</span></span><br><span class="line">    <span class="type">double</span> a=((line1[<span class="number">1</span>]-line2[<span class="number">1</span>])^(line2[<span class="number">2</span>]-line2[<span class="number">1</span>]))*((line1[<span class="number">2</span>]-line2[<span class="number">1</span>])^(line2[<span class="number">2</span>]-line2[<span class="number">1</span>]));</span><br><span class="line">    <span class="type">double</span> b=((line2[<span class="number">2</span>]-line1[<span class="number">1</span>])^(line1[<span class="number">2</span>]-line1[<span class="number">1</span>]))*((line2[<span class="number">1</span>]-line1[<span class="number">1</span>])^(line1[<span class="number">2</span>]-line1[<span class="number">1</span>]));</span><br><span class="line">    <span class="keyword">if</span>(a&lt;=<span class="number">0</span>&amp;&amp;b&lt;=<span class="number">0</span>)<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">double</span> <span class="title function_">abscal</span><span class="params">(Point a)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">sqrt</span>(a.x*a.x+a.y*a.y);</span><br><span class="line">&#125;</span><br><span class="line">Point <span class="title function_">get_cross</span><span class="params">()</span>&#123;</span><br><span class="line">    Point base=line2[<span class="number">2</span>]-line2[<span class="number">1</span>];</span><br><span class="line">    <span class="type">double</span> d1=abscal(base^(line1[<span class="number">1</span>]-line2[<span class="number">1</span>]));</span><br><span class="line">    <span class="type">double</span> d2=abscal(base^(line1[<span class="number">2</span>]-line2[<span class="number">1</span>]));</span><br><span class="line">    <span class="type">double</span> t=d1/(d1+d2);</span><br><span class="line">    Point tmp=line1[<span class="number">2</span>]-line1[<span class="number">1</span>];</span><br><span class="line">    tmp.x*=t;</span><br><span class="line">    tmp.y*=t;</span><br><span class="line">    <span class="keyword">return</span> line1[<span class="number">1</span>]+tmp;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">double</span> <span class="title function_">Dis</span><span class="params">(Point A,Point B)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">sqrt</span>((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//点到直线距离</span></span><br><span class="line"><span class="comment">//返回为result,是点到直线最近的点</span></span><br><span class="line">Point <span class="title function_">PointToLine</span><span class="params">(Point P, Line L)</span></span><br><span class="line">&#123;</span><br><span class="line">	Point result;</span><br><span class="line">	<span class="type">double</span> t = ((P - L.s)*(L.e - L.s)) / ((L.e - L.s)*(L.e - L.s));</span><br><span class="line">	result.x = L.s.x + (L.e.x - L.s.x)*t;</span><br><span class="line">	result.y = L.s.y + (L.e.y - L.s.y)*t;</span><br><span class="line">	<span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//点到线段的距离</span></span><br><span class="line"><span class="comment">//返回点到线段最近的点</span></span><br><span class="line">Point <span class="title function_">NearestPointToLineSeg</span><span class="params">(Point P, Line L)</span></span><br><span class="line">&#123;</span><br><span class="line">	Point result;</span><br><span class="line">	<span class="type">double</span> t = ((P - L.s)*(L.e - L.s)) / ((L.e - L.s)*(L.e - L.s));</span><br><span class="line">	<span class="keyword">if</span> (t &gt;= <span class="number">0</span> &amp;&amp; t &lt;= <span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		result.x = L.s.x + (L.e.x - L.s.x)*t;</span><br><span class="line">		result.y = L.s.y + (L.e.y - L.s.y)*t;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (dist(P, L.s) &lt; dist(P, L.e))</span><br><span class="line">			result = L.s;</span><br><span class="line">		<span class="keyword">else</span> result = L.e;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> a,b,c,d;</span><br><span class="line">    <span class="keyword">while</span>(~<span class="built_in">scanf</span>(<span class="string">&quot;%d%d%d%d&quot;</span>,&amp;a,&amp;b,&amp;c,&amp;d))&#123;</span><br><span class="line">        line1[<span class="number">1</span>]=Point(a,b);</span><br><span class="line">        line1[<span class="number">2</span>]=Point(c,d);</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d%d%d%d&quot;</span>,&amp;a,&amp;b,&amp;c,&amp;d);</span><br><span class="line">        line2[<span class="number">1</span>]=Point(a,b);</span><br><span class="line">        line2[<span class="number">2</span>]=Point(c,d);</span><br><span class="line">        sort(line1+<span class="number">1</span>,line1+<span class="number">3</span>,myCmp);</span><br><span class="line">        sort(line2+<span class="number">1</span>,line2+<span class="number">3</span>,myCmp);</span><br><span class="line">        <span class="keyword">if</span>((line1[<span class="number">2</span>].y-line1[<span class="number">1</span>].y)*(line2[<span class="number">2</span>].x-line2[<span class="number">1</span>].x)==(line1[<span class="number">2</span>].x-line1[<span class="number">1</span>].x)*(line2[<span class="number">2</span>].y-line2[<span class="number">1</span>].y))&#123;</span><br><span class="line">            <span class="keyword">if</span>(line1[<span class="number">2</span>]==line2[<span class="number">1</span>])&#123;</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">&quot;%.2lf %.2lf\n&quot;</span>,line1[<span class="number">2</span>].x,line1[<span class="number">2</span>].y);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">&quot;none\n&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(check())&#123;</span><br><span class="line">                Point tmp=get_cross();</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">&quot;%.2lf %.2lf\n&quot;</span>,tmp.x,tmp.y);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">&quot;none\n&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="点的重定义"><a href="#点的重定义" class="headerlink" title="点的重定义"></a>点的重定义</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">(<span class="number">1</span>) 点的定义</span><br><span class="line"><span class="type">const</span> <span class="type">double</span> eps = <span class="number">1e-8</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">double</span> PI = <span class="built_in">acos</span>(<span class="number">-1.0</span>);</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">sgn</span><span class="params">(<span class="type">double</span> x)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">fabs</span>(x) &lt; eps)<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">if</span> (x &lt; <span class="number">0</span>)<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Point</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">double</span> x, y;</span><br><span class="line">	<span class="built_in">Point</span>() &#123;&#125;</span><br><span class="line">	<span class="built_in">Point</span>(<span class="type">double</span> _x, <span class="type">double</span> _y)</span><br><span class="line">	&#123;</span><br><span class="line">		x = _x; y = _y;</span><br><span class="line">	&#125;</span><br><span class="line">	Point <span class="keyword">operator</span> -(<span class="type">const</span> Point &amp;b)<span class="type">const</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="built_in">Point</span>(x - b.x, y - b.y);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//叉积</span></span><br><span class="line">	<span class="type">double</span> <span class="keyword">operator</span> ^(<span class="type">const</span> Point &amp;b)<span class="type">const</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> x * b.y - y * b.x;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//点积</span></span><br><span class="line">	<span class="type">double</span> <span class="keyword">operator</span> *(<span class="type">const</span> Point &amp;b)<span class="type">const</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> x * b.x + y * b.y;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//绕原点旋转角度B（弧度值），后x,y的变化</span></span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">transXY</span><span class="params">(<span class="type">double</span> B)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="type">double</span> tx = x, ty = y;</span><br><span class="line">		x = tx * <span class="built_in">cos</span>(B) - ty * <span class="built_in">sin</span>(B);</span><br><span class="line">		y = tx * <span class="built_in">sin</span>(B) + ty * <span class="built_in">cos</span>(B);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h4 id="旋转卡壳"><a href="#旋转卡壳" class="headerlink" title="旋转卡壳"></a>旋转卡壳</h4><h5 id="平面最远点对"><a href="#平面最远点对" class="headerlink" title="平面最远点对"></a>平面最远点对</h5><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> eps 1e-6</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Point</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">double</span> x, y;</span><br><span class="line">	<span class="built_in">Point</span>(<span class="type">double</span> _x = <span class="number">0</span>, <span class="type">double</span> _y = <span class="number">0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		x = _x; y = _y;</span><br><span class="line">	&#125;</span><br><span class="line">	Point <span class="keyword">operator</span> -(<span class="type">const</span> Point &amp;b)<span class="type">const</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="built_in">Point</span>(x - b.x, y - b.y);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="type">double</span> <span class="keyword">operator</span> ^(<span class="type">const</span> Point &amp;b)<span class="type">const</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> x * b.y - y * b.x;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="type">double</span> <span class="keyword">operator</span> *(<span class="type">const</span> Point &amp;b)<span class="type">const</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> x * b.x + y * b.y;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">input</span><span class="params">()</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%lf%lf&quot;</span>, &amp;x, &amp;y);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//距离的平方</span></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">dist2</span><span class="params">(Point a, Point b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> (a - b)*(a - b);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//******二维凸包，int***********</span></span><br><span class="line"><span class="type">const</span> <span class="type">int</span> MAXN = <span class="number">100005</span>;</span><br><span class="line">Point list[MAXN];</span><br><span class="line"><span class="type">int</span> Stack[MAXN], top;</span><br><span class="line"><span class="type">bool</span> _cmp(Point p1, Point p2)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">double</span> tmp = (p1 - list[<span class="number">0</span>]) ^ (p2 - list[<span class="number">0</span>]);</span><br><span class="line">	<span class="keyword">if</span> (tmp &gt; eps) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">fabs</span>(tmp) &lt; eps &amp;&amp; <span class="built_in">dist2</span>(p1, list[<span class="number">0</span>]) &lt;= <span class="built_in">dist2</span>(p2, list[<span class="number">0</span>]))</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">Graham</span><span class="params">(<span class="type">int</span> n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	Point p0;</span><br><span class="line">	<span class="type">int</span> k = <span class="number">0</span>;</span><br><span class="line">	p0 = list[<span class="number">0</span>];</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt; n; i++)</span><br><span class="line">		<span class="keyword">if</span> (p0.y &gt; list[i].y || (p0.y == list[i].y &amp;&amp; p0.x &gt; list[i].x))</span><br><span class="line">		&#123;</span><br><span class="line">			p0 = list[i];</span><br><span class="line">			k = i;</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="built_in">swap</span>(list[k], list[<span class="number">0</span>]);</span><br><span class="line">	<span class="built_in">sort</span>(list + <span class="number">1</span>, list + n, _cmp);</span><br><span class="line">	<span class="keyword">if</span> (n == <span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		top = <span class="number">1</span>;</span><br><span class="line">		Stack[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (n == <span class="number">2</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		top = <span class="number">2</span>;</span><br><span class="line">		Stack[<span class="number">0</span>] = <span class="number">0</span>; Stack[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	Stack[<span class="number">0</span>] = <span class="number">0</span>; Stack[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">	top = <span class="number">2</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">2</span>; i &lt; n; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">while</span> (top &gt; <span class="number">1</span> &amp;&amp; ((list[Stack[top - <span class="number">1</span>]] - list[Stack[top - <span class="number">2</span>]]) ^ (list[i] - list[Stack[top - <span class="number">2</span>]])) &lt;= <span class="number">0</span>)</span><br><span class="line">			top--;</span><br><span class="line">		Stack[top++] = i;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//旋转卡壳，求两点间距离平方的最大值</span></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">rotating_calipers</span><span class="params">(Point p[], <span class="type">int</span> n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">double</span> ans = <span class="number">0</span>;</span><br><span class="line">	Point v;</span><br><span class="line">	<span class="type">int</span> cur = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		v = p[i] - p[(i + <span class="number">1</span>) % n];</span><br><span class="line">		<span class="keyword">while</span> ((v ^ (p[(cur + <span class="number">1</span>) % n] - p[cur])) &lt; <span class="number">0</span>)</span><br><span class="line">			cur = (cur + <span class="number">1</span>) % n;</span><br><span class="line">		ans = <span class="built_in">max</span>(ans, <span class="built_in">max</span>(<span class="built_in">dist2</span>(p[i], p[cur]), <span class="built_in">dist2</span>(p[(i + <span class="number">1</span>) % n], p[(cur + <span class="number">1</span>) % n])));</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> ans;</span><br><span class="line">&#125;</span><br><span class="line">Point p[MAXN];</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> n;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;n);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)list[i].<span class="built_in">input</span>();</span><br><span class="line">	<span class="built_in">Graham</span>(n);</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; top; i++) p[i] = list[Stack[i]];</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%.6lf&quot;</span>, <span class="built_in">pow</span>(<span class="built_in">rotating_calipers</span>(p, top),<span class="number">0.5</span>));</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>c语言版本</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> eps 1e-6</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">double</span> x, y;</span><br><span class="line">	Point(<span class="type">double</span> _x = <span class="number">0</span>, <span class="type">double</span> _y = <span class="number">0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		x = _x; y = _y;</span><br><span class="line">	&#125;</span><br><span class="line">	Point operator -(<span class="type">const</span> Point &amp;b)<span class="type">const</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> Point(x - b.x, y - b.y);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="type">double</span> operator ^(<span class="type">const</span> Point &amp;b)<span class="type">const</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> x * b.y - y * b.x;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="type">double</span> operator *(<span class="type">const</span> Point &amp;b)<span class="type">const</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> x * b.x + y * b.y;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="type">void</span> <span class="title function_">input</span><span class="params">()</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%lf%lf&quot;</span>, &amp;x, &amp;y);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//距离的平方</span></span><br><span class="line"><span class="type">double</span> <span class="title function_">dist2</span><span class="params">(Point a, Point b)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> (a - b)*(a - b);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//******二维凸包，int***********</span></span><br><span class="line"><span class="type">const</span> <span class="type">int</span> MAXN = <span class="number">100005</span>;</span><br><span class="line">Point <span class="built_in">list</span>[MAXN];</span><br><span class="line"><span class="type">int</span> Stack[MAXN], top;</span><br><span class="line"><span class="type">bool</span> _cmp(Point p1, Point p2)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">double</span> tmp = (p1 - <span class="built_in">list</span>[<span class="number">0</span>]) ^ (p2 - <span class="built_in">list</span>[<span class="number">0</span>]);</span><br><span class="line">	<span class="keyword">if</span> (tmp &gt; eps) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">fabs</span>(tmp) &lt; eps &amp;&amp; dist2(p1, <span class="built_in">list</span>[<span class="number">0</span>]) &lt;= dist2(p2, <span class="built_in">list</span>[<span class="number">0</span>]))</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">void</span> <span class="title function_">Graham</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">&#123;</span><br><span class="line">	Point p0;</span><br><span class="line">	<span class="type">int</span> k = <span class="number">0</span>;</span><br><span class="line">	p0 = <span class="built_in">list</span>[<span class="number">0</span>];</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt; n; i++)</span><br><span class="line">		<span class="keyword">if</span> (p0.y &gt; <span class="built_in">list</span>[i].y || (p0.y == <span class="built_in">list</span>[i].y &amp;&amp; p0.x &gt; <span class="built_in">list</span>[i].x))</span><br><span class="line">		&#123;</span><br><span class="line">			p0 = <span class="built_in">list</span>[i];</span><br><span class="line">			k = i;</span><br><span class="line">		&#125;</span><br><span class="line">	swap(<span class="built_in">list</span>[k], <span class="built_in">list</span>[<span class="number">0</span>]);</span><br><span class="line">	sort(<span class="built_in">list</span> + <span class="number">1</span>, <span class="built_in">list</span> + n, _cmp);</span><br><span class="line">	<span class="keyword">if</span> (n == <span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		top = <span class="number">1</span>;</span><br><span class="line">		Stack[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (n == <span class="number">2</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		top = <span class="number">2</span>;</span><br><span class="line">		Stack[<span class="number">0</span>] = <span class="number">0</span>; Stack[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	Stack[<span class="number">0</span>] = <span class="number">0</span>; Stack[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">	top = <span class="number">2</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">2</span>; i &lt; n; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">while</span> (top &gt; <span class="number">1</span> &amp;&amp; ((<span class="built_in">list</span>[Stack[top - <span class="number">1</span>]] - <span class="built_in">list</span>[Stack[top - <span class="number">2</span>]]) ^ (<span class="built_in">list</span>[i] - <span class="built_in">list</span>[Stack[top - <span class="number">2</span>]])) &lt;= <span class="number">0</span>)</span><br><span class="line">			top--;</span><br><span class="line">		Stack[top++] = i;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//旋转卡壳，求两点间距离平方的最大值</span></span><br><span class="line"><span class="type">double</span> <span class="title function_">rotating_calipers</span><span class="params">(Point p[], <span class="type">int</span> n)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">double</span> ans = <span class="number">0</span>;</span><br><span class="line">	Point v;</span><br><span class="line">	<span class="type">int</span> cur = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		v = p[i] - p[(i + <span class="number">1</span>) % n];</span><br><span class="line">		<span class="keyword">while</span> ((v ^ (p[(cur + <span class="number">1</span>) % n] - p[cur])) &lt; <span class="number">0</span>)</span><br><span class="line">			cur = (cur + <span class="number">1</span>) % n;</span><br><span class="line">		ans = max(ans, max(dist2(p[i], p[cur]), dist2(p[(i + <span class="number">1</span>) % n], p[(cur + <span class="number">1</span>) % n])));</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> ans;</span><br><span class="line">&#125;</span><br><span class="line">Point p[MAXN];</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> n;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;n);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)<span class="built_in">list</span>[i].input();</span><br><span class="line">	Graham(n);</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; top; i++) p[i] = <span class="built_in">list</span>[Stack[i]];</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%.6lf&quot;</span>, <span class="built_in">pow</span>(rotating_calipers(p, top),<span class="number">0.5</span>));</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h5 id="求解平面点集最大三角形"><a href="#求解平面点集最大三角形" class="headerlink" title="求解平面点集最大三角形"></a>求解平面点集最大三角形</h5><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">//旋转卡壳计算平面点集最大三角形面积</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">rotating_calipers</span><span class="params">(Point p[], <span class="type">int</span> n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> ans = <span class="number">0</span>;</span><br><span class="line">	Point v;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="type">int</span> j = (i + <span class="number">1</span>) % n;</span><br><span class="line">		<span class="type">int</span> k = (j + <span class="number">1</span>) % n;</span><br><span class="line">		<span class="keyword">while</span> (j != i &amp;&amp; k != i)</span><br><span class="line">		&#123;</span><br><span class="line">			ans = <span class="built_in">max</span>(ans, <span class="built_in">abs</span>((p[j] - p[i]) ^ (p[k] - p[i])));</span><br><span class="line">			<span class="keyword">while</span> (((p[i] - p[j]) ^ (p[(k + <span class="number">1</span>) % n] - p[k])) &lt; <span class="number">0</span>)</span><br><span class="line">				k = (k + <span class="number">1</span>) % n;</span><br><span class="line">			j = (j + <span class="number">1</span>) % n;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> ans;</span><br><span class="line">&#125;</span><br><span class="line">Point p[MAXN];</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> n;</span><br><span class="line">	<span class="keyword">while</span> (<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;n) == <span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (n == <span class="number">-1</span>)<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)list[i].<span class="built_in">input</span>();</span><br><span class="line">		<span class="built_in">Graham</span>(n);</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; top; i++)p[i] = list[Stack[i]];</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%.2f\n&quot;</span>, (<span class="type">double</span>)<span class="built_in">rotating_calipers</span>(p, top) / <span class="number">2</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="FFT"><a href="#FFT" class="headerlink" title="FFT"></a>FFT</h2><h3 id="多项式乘"><a href="#多项式乘" class="headerlink" title="多项式乘"></a>多项式乘</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> Author: 林智灏</span></span><br><span class="line"><span class="comment"> Result: AC	Submission_id: 4057777</span></span><br><span class="line"><span class="comment"> Created at: Mon Nov 29 2021 16:29:45 GMT+0800 (China Standard Time)</span></span><br><span class="line"><span class="comment"> Problem_id: 5251	Time: 572	Memory: 45660</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> Maxn 1350000</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="type">const</span> <span class="type">double</span> Pi=<span class="built_in">acos</span>(<span class="number">-1</span>);</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="type">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="keyword">register</span> <span class="type">char</span> ch=<span class="number">0</span>;</span><br><span class="line">  <span class="keyword">while</span>(ch&lt;<span class="number">48</span>||ch&gt;<span class="number">57</span>)ch=<span class="built_in">getchar</span>();</span><br><span class="line">  <span class="keyword">return</span> ch-<span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> n,m;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">CP</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="built_in">CP</span> (<span class="type">double</span> xx=<span class="number">0</span>,<span class="type">double</span> yy=<span class="number">0</span>)&#123;x=xx,y=yy;&#125;</span><br><span class="line">  <span class="type">double</span> x,y;</span><br><span class="line">  CP <span class="keyword">operator</span> + (CP <span class="type">const</span> &amp;B) <span class="type">const</span></span><br><span class="line">  &#123;<span class="keyword">return</span> <span class="built_in">CP</span>(x+B.x,y+B.y);&#125;</span><br><span class="line">  CP <span class="keyword">operator</span> - (CP <span class="type">const</span> &amp;B) <span class="type">const</span></span><br><span class="line">  &#123;<span class="keyword">return</span> <span class="built_in">CP</span>(x-B.x,y-B.y);&#125;</span><br><span class="line">  CP <span class="keyword">operator</span> * (CP <span class="type">const</span> &amp;B) <span class="type">const</span></span><br><span class="line">  &#123;<span class="keyword">return</span> <span class="built_in">CP</span>(x*B.x-y*B.y,x*B.y+y*B.x);&#125;</span><br><span class="line">&#125;f[Maxn&lt;&lt;<span class="number">1</span>];<span class="comment">//只用了一个复数数组 </span></span><br><span class="line"><span class="type">int</span> tr[Maxn&lt;&lt;<span class="number">1</span>];</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">fft</span><span class="params">(CP *f,<span class="type">bool</span> flag)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>;i&lt;n;i++)</span><br><span class="line">    <span class="keyword">if</span> (i&lt;tr[i])<span class="built_in">swap</span>(f[i],f[tr[i]]);</span><br><span class="line">  <span class="keyword">for</span>(<span class="type">int</span> p=<span class="number">2</span>;p&lt;=n;p&lt;&lt;=<span class="number">1</span>)&#123;</span><br><span class="line">    <span class="type">int</span> len=p&gt;&gt;<span class="number">1</span>;</span><br><span class="line">    <span class="function">CP <span class="title">tG</span><span class="params">(cos(<span class="number">2</span>*Pi/p),sin(<span class="number">2</span>*Pi/p))</span></span>;</span><br><span class="line">    <span class="keyword">if</span>(!flag)tG.y*=<span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> k=<span class="number">0</span>;k&lt;n;k+=p)&#123;</span><br><span class="line">      <span class="function">CP <span class="title">buf</span><span class="params">(<span class="number">1</span>,<span class="number">0</span>)</span></span>;</span><br><span class="line">      <span class="keyword">for</span>(<span class="type">int</span> l=k;l&lt;k+len;l++)&#123;</span><br><span class="line">        CP tt=buf*f[len+l];</span><br><span class="line">        f[len+l]=f[l]-tt;</span><br><span class="line">        f[l]=f[l]+tt;</span><br><span class="line">        buf=buf*tG;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;n,&amp;m);</span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>;i&lt;=n;i++)f[i].x=<span class="built_in">read</span>();</span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>;i&lt;=m;i++)f[i].y=<span class="built_in">read</span>();</span><br><span class="line">  <span class="keyword">for</span>(m+=n,n=<span class="number">1</span>;n&lt;=m;n&lt;&lt;=<span class="number">1</span>);</span><br><span class="line">  <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;n;i++)</span><br><span class="line">    tr[i]=(tr[i&gt;&gt;<span class="number">1</span>]&gt;&gt;<span class="number">1</span>)|((i&amp;<span class="number">1</span>)?n&gt;&gt;<span class="number">1</span>:<span class="number">0</span>);</span><br><span class="line">  <span class="built_in">fft</span>(f,<span class="number">1</span>);</span><br><span class="line">  <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;n;++i)f[i]=f[i]*f[i];</span><br><span class="line">  <span class="built_in">fft</span>(f,<span class="number">0</span>);</span><br><span class="line">  <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;=m;++i)</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>,(<span class="type">int</span>)(f[i].y/n/<span class="number">2</span>+<span class="number">0.49</span>));</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="高精度乘法"><a href="#高精度乘法" class="headerlink" title="高精度乘法"></a>高精度乘法</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="type">const</span> <span class="type">double</span> PI = <span class="built_in">acos</span>(<span class="number">-1.0</span>);</span><br><span class="line"><span class="comment">//复数结构体</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Complex</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">double</span> x, y;<span class="comment">//实部和虚部 x+yi</span></span><br><span class="line">	<span class="built_in">Complex</span>(<span class="type">double</span> _x = <span class="number">0.0</span>, <span class="type">double</span> _y = <span class="number">0.0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		x = _x;</span><br><span class="line">		y = _y;</span><br><span class="line">	&#125;</span><br><span class="line">	Complex <span class="keyword">operator</span> -(<span class="type">const</span> Complex &amp;b)<span class="type">const</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="built_in">Complex</span>(x - b.x, y - b.y);</span><br><span class="line">	&#125;</span><br><span class="line">	Complex <span class="keyword">operator</span> +(<span class="type">const</span> Complex &amp;b)<span class="type">const</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="built_in">Complex</span>(x + b.x, y + b.y);</span><br><span class="line">	&#125;</span><br><span class="line">	Complex <span class="keyword">operator</span> *(<span class="type">const</span> Complex &amp;b)<span class="type">const</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="built_in">Complex</span>(x*b.x - y * b.y, x*b.y + y * b.x);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">* 进行FFT和IFFT前的反转变换。</span></span><br><span class="line"><span class="comment">* 位置i和 （i二进制反转后位置）互换</span></span><br><span class="line"><span class="comment">* len必须去2的幂</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">change</span><span class="params">(Complex y[], <span class="type">int</span> len)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> i, j, k;</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">1</span>, j = len / <span class="number">2</span>; i &lt; len - <span class="number">1</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (i &lt; j)<span class="built_in">swap</span>(y[i], y[j]);</span><br><span class="line">		<span class="comment">//交换互为小标反转的元素，i&lt;j保证交换一次</span></span><br><span class="line">		<span class="comment">//i做正常的+1，j左反转类型的+1,始终保持i和j是反转的</span></span><br><span class="line">		k = len / <span class="number">2</span>;</span><br><span class="line">		<span class="keyword">while</span> (j &gt;= k)</span><br><span class="line">		&#123;</span><br><span class="line">			j -= k;</span><br><span class="line">			k /= <span class="number">2</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (j &lt; k)j += k;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">* 做FFT</span></span><br><span class="line"><span class="comment">* len必须为2^k形式，</span></span><br><span class="line"><span class="comment">* on==1时是DFT，on==-1时是IDFT</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">fft</span><span class="params">(Complex y[], <span class="type">int</span> len, <span class="type">int</span> on)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">change</span>(y, len);</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> h = <span class="number">2</span>; h &lt;= len; h &lt;&lt;= <span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="function">Complex <span class="title">wn</span><span class="params">(cos(-on * <span class="number">2</span> * PI / h), sin(-on * <span class="number">2</span> * PI / h))</span></span>;</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; len; j += h)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="function">Complex <span class="title">w</span><span class="params">(<span class="number">1</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line">			<span class="keyword">for</span> (<span class="type">int</span> k = j; k &lt; j + h / <span class="number">2</span>; k++)</span><br><span class="line">			&#123;</span><br><span class="line">				Complex u = y[k];</span><br><span class="line">				Complex t = w * y[k + h / <span class="number">2</span>];</span><br><span class="line">				y[k] = u + t;</span><br><span class="line">				y[k + h / <span class="number">2</span>] = u - t;</span><br><span class="line">				w = w * wn;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (on == <span class="number">-1</span>)</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i++)</span><br><span class="line">			y[i].x /= len;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> MAXN = <span class="number">200010</span>;</span><br><span class="line">Complex x1[MAXN], x2[MAXN];</span><br><span class="line"><span class="type">char</span> str1[MAXN / <span class="number">2</span>], str2[MAXN / <span class="number">2</span>];</span><br><span class="line"><span class="type">int</span> sum[MAXN];</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">while</span> (<span class="built_in">scanf</span>(<span class="string">&quot;%s%s&quot;</span>, str1, str2) == <span class="number">2</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="type">int</span> len1 = <span class="built_in">strlen</span>(str1);</span><br><span class="line">		<span class="type">int</span> len2 = <span class="built_in">strlen</span>(str2);</span><br><span class="line">		<span class="type">int</span> len = <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">while</span> (len &lt; len1 * <span class="number">2</span> || len &lt; len2 * <span class="number">2</span>)len &lt;&lt;= <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len1; i++)</span><br><span class="line">			x1[i] = <span class="built_in">Complex</span>(str1[len1 - <span class="number">1</span> - i] - <span class="string">&#x27;0&#x27;</span>, <span class="number">0</span>);</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = len1; i &lt; len; i++)</span><br><span class="line">			x1[i] = <span class="built_in">Complex</span>(<span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len2; i++)</span><br><span class="line">			x2[i] = <span class="built_in">Complex</span>(str2[len2 - <span class="number">1</span> - i] - <span class="string">&#x27;0&#x27;</span>, <span class="number">0</span>);</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = len2; i &lt; len; i++)</span><br><span class="line">			x2[i] = <span class="built_in">Complex</span>(<span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">		<span class="comment">//求DFT</span></span><br><span class="line">		<span class="built_in">fft</span>(x1, len, <span class="number">1</span>);</span><br><span class="line">		<span class="built_in">fft</span>(x2, len, <span class="number">1</span>);</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i++)</span><br><span class="line">			x1[i] = x1[i] * x2[i];</span><br><span class="line">		<span class="built_in">fft</span>(x1, len, <span class="number">-1</span>);</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i++)</span><br><span class="line">			sum[i] = (<span class="type">int</span>)(x1[i].x + <span class="number">0.5</span>);</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i++)</span><br><span class="line">		&#123;</span><br><span class="line">			sum[i + <span class="number">1</span>] += sum[i] / <span class="number">10</span>;</span><br><span class="line">			sum[i] %= <span class="number">10</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		len = len1 + len2 - <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">while</span> (sum[len] &lt;= <span class="number">0</span> &amp;&amp; len &gt; <span class="number">0</span>)len--;</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = len; i &gt;= <span class="number">0</span>; i--)</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>, sum[i] + <span class="string">&#x27;0&#x27;</span>);</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>c语言版本</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">const</span> <span class="type">double</span> PI = <span class="built_in">acos</span>(<span class="number">-1.0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 复数结构体</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">    <span class="type">double</span> x, y;  <span class="comment">// 实部和虚部 x+yi</span></span><br><span class="line">&#125; Complex;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 复数加法</span></span><br><span class="line">Complex <span class="title function_">complex_add</span><span class="params">(Complex a, Complex b)</span> &#123;</span><br><span class="line">    Complex result;</span><br><span class="line">    result.x = a.x + b.x;</span><br><span class="line">    result.y = a.y + b.y;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 复数减法</span></span><br><span class="line">Complex <span class="title function_">complex_sub</span><span class="params">(Complex a, Complex b)</span> &#123;</span><br><span class="line">    Complex result;</span><br><span class="line">    result.x = a.x - b.x;</span><br><span class="line">    result.y = a.y - b.y;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 复数乘法</span></span><br><span class="line">Complex <span class="title function_">complex_mul</span><span class="params">(Complex a, Complex b)</span> &#123;</span><br><span class="line">    Complex result;</span><br><span class="line">    result.x = a.x * b.x - a.y * b.y;</span><br><span class="line">    result.y = a.x * b.y + a.y * b.x;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 交换两个复数</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">swap</span><span class="params">(Complex *a, Complex *b)</span> &#123;</span><br><span class="line">    Complex temp = *a;</span><br><span class="line">    *a = *b;</span><br><span class="line">    *b = temp;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 进行FFT和IFFT前的反转变换</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">change</span><span class="params">(Complex y[], <span class="type">int</span> len)</span> &#123;</span><br><span class="line">    <span class="type">int</span> i, j, k;</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">1</span>, j = len / <span class="number">2</span>; i &lt; len - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (i &lt; j) swap(&amp;y[i], &amp;y[j]);</span><br><span class="line">        k = len / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">while</span> (j &gt;= k) &#123;</span><br><span class="line">            j -= k;</span><br><span class="line">            k /= <span class="number">2</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (j &lt; k) j += k;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// FFT</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">fft</span><span class="params">(Complex y[], <span class="type">int</span> len, <span class="type">int</span> on)</span> &#123;</span><br><span class="line">    change(y, len);</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> h = <span class="number">2</span>; h &lt;= len; h &lt;&lt;= <span class="number">1</span>) &#123;</span><br><span class="line">        Complex wn;</span><br><span class="line">        wn.x = <span class="built_in">cos</span>(-on * <span class="number">2</span> * PI / h);</span><br><span class="line">        wn.y = <span class="built_in">sin</span>(-on * <span class="number">2</span> * PI / h);</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; len; j += h) &#123;</span><br><span class="line">            Complex w;</span><br><span class="line">            w.x = <span class="number">1</span>; w.y = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> k = j; k &lt; j + h / <span class="number">2</span>; k++) &#123;</span><br><span class="line">                Complex u = y[k];</span><br><span class="line">                Complex t = complex_mul(w, y[k + h / <span class="number">2</span>]);</span><br><span class="line">                y[k] = complex_add(u, t);</span><br><span class="line">                y[k + h / <span class="number">2</span>] = complex_sub(u, t);</span><br><span class="line">                w = complex_mul(w, wn);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (on == <span class="number">-1</span>) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i++)</span><br><span class="line">            y[i].x /= len;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">const</span> <span class="type">int</span> MAXN = <span class="number">200010</span>;</span><br><span class="line">Complex x1[MAXN], x2[MAXN];</span><br><span class="line"><span class="type">char</span> str1[MAXN / <span class="number">2</span>], str2[MAXN / <span class="number">2</span>];</span><br><span class="line"><span class="type">int</span> sum[MAXN];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">while</span> (<span class="built_in">scanf</span>(<span class="string">&quot;%s%s&quot;</span>, str1, str2) == <span class="number">2</span>) &#123;</span><br><span class="line">        <span class="type">int</span> len1 = <span class="built_in">strlen</span>(str1);</span><br><span class="line">        <span class="type">int</span> len2 = <span class="built_in">strlen</span>(str2);</span><br><span class="line">        <span class="type">int</span> len = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span> (len &lt; len1 * <span class="number">2</span> || len &lt; len2 * <span class="number">2</span>) len &lt;&lt;= <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len1; i++) &#123;</span><br><span class="line">            x1[i].x = str1[len1 - <span class="number">1</span> - i] - <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">            x1[i].y = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = len1; i &lt; len; i++) &#123;</span><br><span class="line">            x1[i].x = <span class="number">0</span>; x1[i].y = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len2; i++) &#123;</span><br><span class="line">            x2[i].x = str2[len2 - <span class="number">1</span> - i] - <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">            x2[i].y = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = len2; i &lt; len; i++) &#123;</span><br><span class="line">            x2[i].x = <span class="number">0</span>; x2[i].y = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        fft(x1, len, <span class="number">1</span>);</span><br><span class="line">        fft(x2, len, <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i++)</span><br><span class="line">            x1[i] = complex_mul(x1[i], x2[i]);</span><br><span class="line">        fft(x1, len, <span class="number">-1</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i++)</span><br><span class="line">            sum[i] = (<span class="type">int</span>)(x1[i].x + <span class="number">0.5</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">            sum[i + <span class="number">1</span>] += sum[i] / <span class="number">10</span>;</span><br><span class="line">            sum[i] %= <span class="number">10</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        len = len1 + len2 - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span> (sum[len] &lt;= <span class="number">0</span> &amp;&amp; len &gt; <span class="number">0</span>) len--;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = len; i &gt;= <span class="number">0</span>; i--)</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>, sum[i] + <span class="string">&#x27;0&#x27;</span>);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h1 id="其他-1"><a href="#其他-1" class="headerlink" title="其他"></a>其他</h1><h2 id="二分答案求最大值中的最小值"><a href="#二分答案求最大值中的最小值" class="headerlink" title="二分答案求最大值中的最小值"></a>二分答案求最大值中的最小值</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> a[<span class="number">100005</span>];</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> n,k;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;n,&amp;k);</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="type">int</span> max=<span class="number">0</span>;</span><br><span class="line">	<span class="type">int</span> min=<span class="number">-99999</span>;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;a[i]);</span><br><span class="line">		max+=a[i];</span><br><span class="line">		<span class="keyword">if</span>(a[i]&gt;min) min = a[i];</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">while</span>(min&lt;max)&#123;</span><br><span class="line">		<span class="type">int</span> cnt=<span class="number">1</span>,sum=<span class="number">0</span>;</span><br><span class="line">		<span class="type">int</span> mid = (min+max)/<span class="number">2</span>;</span><br><span class="line">		<span class="type">int</span> j;</span><br><span class="line">		<span class="keyword">for</span>(j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(sum+a[j]&gt;mid)&#123;</span><br><span class="line">				cnt++;</span><br><span class="line">				sum = a[j];</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span> sum+=a[j];</span><br><span class="line">		&#125;	</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span>(cnt&lt;=k) max = mid;</span><br><span class="line">		<span class="keyword">else</span> min = mid+<span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,max);</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="欧拉回路"><a href="#欧拉回路" class="headerlink" title="欧拉回路"></a>欧拉回路</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fstream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;deque&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;list&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PI acos(-1.0)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> mem(a,b) memset(a,b,sizeof(a))</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> sca(a) scanf(<span class="string">&quot;%d&quot;</span>,&amp;a)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> pri(a) printf(<span class="string">&quot;%d\n&quot;</span>,a)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> M 202</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INF 100000001</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">stack</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> top,node[M];</span><br><span class="line">&#125;s;</span><br><span class="line"><span class="type">int</span> e[M][M],n;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">dfs</span><span class="params">(<span class="type">int</span> x)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line">    s.node[++s.top]=x;</span><br><span class="line">    <span class="comment">//cout&lt;&lt;s.top&lt;&lt;&#x27; &#x27;&lt;&lt;s.node[s.top]+1&lt;&lt;endl;</span></span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;n;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span>(e[i][x]&gt;<span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            e[i][x]=e[x][i]=<span class="number">0</span>;  <span class="comment">//删除这条边</span></span><br><span class="line">            <span class="built_in">dfs</span>(i);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">fleury</span><span class="params">(<span class="type">int</span> x)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i,flag;</span><br><span class="line">    s.top=<span class="number">0</span>; s.node[s.top]=x;</span><br><span class="line">    <span class="keyword">while</span>(s.top&gt;=<span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        flag=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;n;i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span>(e[s.node[s.top]][i]&gt;<span class="number">0</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                flag=<span class="number">1</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(!flag) <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>,s.node[s.top--]+<span class="number">1</span>);</span><br><span class="line">        <span class="keyword">else</span> <span class="built_in">dfs</span>(s.node[s.top--]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">puts</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">( )</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i,j,u,v,m,degree,num=<span class="number">0</span>,start=<span class="number">0</span>;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;n,&amp;m);</span><br><span class="line">    <span class="built_in">mem</span>(e,<span class="number">0</span>);</span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;m;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;u,&amp;v);</span><br><span class="line">        e[u<span class="number">-1</span>][v<span class="number">-1</span>]=e[v<span class="number">-1</span>][u<span class="number">-1</span>]=<span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;n;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        degree=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(j=<span class="number">0</span>;j&lt;n;j++)</span><br><span class="line">            degree+=e[i][j];</span><br><span class="line">        <span class="keyword">if</span>(degree&amp;<span class="number">1</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            start=i;</span><br><span class="line">            num++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(num==<span class="number">0</span>||num==<span class="number">2</span>) <span class="built_in">fleury</span>(start);</span><br><span class="line">    <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">&quot;No Euler path\n&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="KMP字符串匹配优化"><a href="#KMP字符串匹配优化" class="headerlink" title="KMP字符串匹配优化"></a>KMP字符串匹配优化</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ll long long</span></span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"><span class="type">int</span> next[<span class="number">10005</span>];</span><br><span class="line"><span class="type">void</span> <span class="title function_">GetNext</span><span class="params">(<span class="type">char</span>* p)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> pLen = <span class="built_in">strlen</span>(p);</span><br><span class="line">	next[<span class="number">0</span>] = <span class="number">-1</span>;</span><br><span class="line">	<span class="type">int</span> k = <span class="number">-1</span>;</span><br><span class="line">	<span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span> (j &lt; pLen - <span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//p[k]表示前缀，p[j]表示后缀  </span></span><br><span class="line">		<span class="keyword">if</span> (k == <span class="number">-1</span> || p[j] == p[k])</span><br><span class="line">		&#123;</span><br><span class="line">			++j;</span><br><span class="line">			++k;</span><br><span class="line">			<span class="comment">//较之前next数组求法，改动在下面4行</span></span><br><span class="line">			<span class="keyword">if</span> (p[j] != p[k])</span><br><span class="line">				next[j] = k;   <span class="comment">//之前只有这一行</span></span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">				<span class="comment">//因为不能出现p[j] = p[ next[j ]]，所以当出现时需要继续递归，k = next[k] = next[next[k]]</span></span><br><span class="line">				next[j] = next[k];</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			k = next[k];</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">KmpSearch</span><span class="params">(<span class="type">char</span>* s, <span class="type">char</span>* p)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">	<span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line">	<span class="type">int</span> sLen = <span class="built_in">strlen</span>(s);</span><br><span class="line">	<span class="type">int</span> pLen = <span class="built_in">strlen</span>(p);</span><br><span class="line">	<span class="keyword">while</span> (i &lt; sLen &amp;&amp; j &lt; pLen)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//①如果j = -1，或者当前字符匹配成功（即S[i] == P[j]），都令i++，j++    </span></span><br><span class="line">		<span class="keyword">if</span> (j == <span class="number">-1</span> || s[i] == p[j])</span><br><span class="line">		&#123;</span><br><span class="line">			i++;</span><br><span class="line">			j++;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="comment">//②如果j != -1，且当前字符匹配失败（即S[i] != P[j]），则令 i 不变，j = next[j]    </span></span><br><span class="line">			<span class="comment">//next[j]即为j所对应的next值      </span></span><br><span class="line">			j = next[j];</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (j == pLen)</span><br><span class="line">		<span class="keyword">return</span> i - j;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	ios::sync_with_stdio(<span class="literal">false</span>);</span><br><span class="line">	<span class="type">char</span> s[<span class="number">100005</span>], p[<span class="number">100005</span>];</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>, s);</span><br><span class="line">	getchar();</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>, p);</span><br><span class="line">	GetNext(p);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>, KmpSearch(s, p));</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>多次匹配</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> next[<span class="number">1000005</span>];</span><br><span class="line"><span class="type">char</span> s[<span class="number">1000005</span>],t[<span class="number">1000005</span>];</span><br><span class="line"><span class="type">int</span> ls,lt;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">build</span><span class="params">(<span class="type">char</span> t[],<span class="type">int</span> n)</span>&#123;</span><br><span class="line">	<span class="type">int</span> k=<span class="number">0</span>;</span><br><span class="line">	next[<span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line">	<span class="type">int</span> i=<span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">2</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">while</span>(k&amp;&amp;t[i]!=t[k+<span class="number">1</span>])&#123;</span><br><span class="line">			k = next[k];</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(t[i]==t[k+<span class="number">1</span>]) k++;</span><br><span class="line">		next[i] = k;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">kmp</span><span class="params">(<span class="type">int</span> begin)</span>&#123;	</span><br><span class="line">	<span class="type">int</span> j=<span class="number">0</span>,i;</span><br><span class="line">	<span class="keyword">for</span>(i=begin;i&lt;=ls;i++)&#123;</span><br><span class="line">		<span class="keyword">while</span>(j&amp;&amp;s[i]!=t[j+<span class="number">1</span>])&#123;</span><br><span class="line">			j = next[j];</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(s[i]==t[j+<span class="number">1</span>]) j++;</span><br><span class="line">		<span class="keyword">if</span>(j==lt)&#123;</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>,i-j);</span><br><span class="line">		&#125; </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>,s+<span class="number">1</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>,t+<span class="number">1</span>);</span><br><span class="line">	ls = <span class="built_in">strlen</span>(s+<span class="number">1</span>);</span><br><span class="line">	lt = <span class="built_in">strlen</span>(t+<span class="number">1</span>);</span><br><span class="line">	</span><br><span class="line">	build(t,lt+<span class="number">1</span>);</span><br><span class="line">	kmp(<span class="number">1</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="求前缀等于后缀"><a href="#求前缀等于后缀" class="headerlink" title="求前缀等于后缀"></a>求前缀等于后缀</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ll long long</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 1000005</span></span><br><span class="line"></span><br><span class="line"><span class="type">char</span> s[N];</span><br><span class="line"><span class="type">int</span> ans[N];</span><br><span class="line"><span class="type">int</span> next[N];</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">build</span><span class="params">(<span class="type">char</span> t[])</span>&#123;</span><br><span class="line">    <span class="type">int</span> n = <span class="built_in">strlen</span>(t);</span><br><span class="line">	<span class="type">int</span> k=<span class="number">0</span>;</span><br><span class="line">	next[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;n;i++)&#123;</span><br><span class="line">		<span class="keyword">while</span>(k&amp;&amp;t[i]!=t[k])&#123;</span><br><span class="line">			k = next[k<span class="number">-1</span>];</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(t[i]==t[k]) k++;</span><br><span class="line">		next[i] = k;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>,s);</span><br><span class="line">	<span class="type">int</span> len = <span class="built_in">strlen</span>(s);</span><br><span class="line">	build(s);</span><br><span class="line">	<span class="type">int</span> i = len<span class="number">-1</span>,cnt=<span class="number">0</span>;</span><br><span class="line">	ans[<span class="number">0</span>] = len;</span><br><span class="line">	<span class="keyword">while</span>(next[i]&gt;<span class="number">0</span>)&#123;</span><br><span class="line">		ans[++cnt] = next[i];</span><br><span class="line">		i = next[i]<span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(i=cnt;i&gt;=<span class="number">0</span>;i--) <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,ans[i]);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="三次方程求根"><a href="#三次方程求根" class="headerlink" title="三次方程求根"></a>三次方程求根</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="type">double</span> a,b,c,d;</span><br><span class="line"><span class="type">double</span> <span class="title function_">fc</span><span class="params">(<span class="type">double</span> x)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> a*x*x*x+b*x*x+c*x+d;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">double</span> l,r,m,x1,x2;</span><br><span class="line">    <span class="type">int</span> s=<span class="number">0</span>,i;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%lf%lf%lf%lf&quot;</span>,&amp;a,&amp;b,&amp;c,&amp;d);  <span class="comment">//输入</span></span><br><span class="line">    <span class="keyword">for</span> (i=<span class="number">-100</span>;i&lt;<span class="number">100</span>;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        l=i; </span><br><span class="line">        r=i+<span class="number">1</span>;</span><br><span class="line">        x1=fc(l); </span><br><span class="line">        x2=fc(r);</span><br><span class="line">        <span class="keyword">if</span>(!x1) </span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%.2lf &quot;</span>,l); </span><br><span class="line">            s++;</span><br><span class="line">        &#125;      <span class="comment">//判断左端点，是零点直接输出。</span></span><br><span class="line">                        </span><br><span class="line">                        <span class="comment">//不能判断右端点，会重复。</span></span><br><span class="line">        <span class="keyword">if</span>(x1*x2&lt;<span class="number">0</span>)                             <span class="comment">//区间内有根。</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">while</span>(r-l&gt;=<span class="number">0.001</span>)                     <span class="comment">//二分控制精度。</span></span><br><span class="line">            &#123;</span><br><span class="line">                m=(l+r)/<span class="number">2</span>;  <span class="comment">//middle</span></span><br><span class="line">                <span class="keyword">if</span>(fc(m)*fc(r)&lt;=<span class="number">0</span>) </span><br><span class="line">                   l=m; </span><br><span class="line">                <span class="keyword">else</span> </span><br><span class="line">                   r=m;   <span class="comment">//计算中点处函数值缩小区间。</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%.2lf &quot;</span>,r);  </span><br><span class="line">            <span class="comment">//输出右端点。</span></span><br><span class="line">            s++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (s==<span class="number">3</span>) </span><br><span class="line">            <span class="keyword">break</span>;             </span><br><span class="line">            <span class="comment">//找到三个就退出大概会省一点时间</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="自然数的拆分"><a href="#自然数的拆分" class="headerlink" title="自然数的拆分"></a>自然数的拆分</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdlib&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="type">int</span> a[<span class="number">10001</span>]=&#123;<span class="number">1</span>&#125;,n;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">search</span><span class="params">(<span class="type">int</span>,<span class="type">int</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">print</span><span class="params">(<span class="type">int</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	cin&gt;&gt;n;</span><br><span class="line">	<span class="built_in">search</span>(n,<span class="number">1</span>);<span class="comment">//将要拆分的数n传递给s</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">search</span><span class="params">(<span class="type">int</span> s,<span class="type">int</span> t)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=a[t<span class="number">-1</span>];i&lt;=s;i++)</span><br><span class="line">		<span class="keyword">if</span>(i&lt;n)<span class="comment">//当前数i要大于等于前一位数，且不超过n</span></span><br><span class="line">		&#123;</span><br><span class="line">			a[t]=i;<span class="comment">//保存当前拆分的数i</span></span><br><span class="line">			s-=i;<span class="comment">//s减去数i，s的值将继续拆分</span></span><br><span class="line">			<span class="keyword">if</span>(s==<span class="number">0</span>)<span class="built_in">print</span>(t);<span class="comment">//当s=0时，拆分结束输出结果</span></span><br><span class="line">				<span class="keyword">else</span> <span class="built_in">search</span>(s,t+<span class="number">1</span>);<span class="comment">//当s&gt;0时，继续递归</span></span><br><span class="line">			s+=i;<span class="comment">//回溯：加上拆分的数，以便产生所有可能的拆分</span></span><br><span class="line">		&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">print</span><span class="params">(<span class="type">int</span> t)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=t<span class="number">-1</span>;i++)<span class="comment">//输出一种拆分方案</span></span><br><span class="line">		cout&lt;&lt;a[i]&lt;&lt;<span class="string">&quot;+&quot;</span>;</span><br><span class="line">	cout&lt;&lt;a[t]&lt;&lt;endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="拓扑排序-1"><a href="#拓扑排序-1" class="headerlink" title="拓扑排序"></a>拓扑排序</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">题目背景</span><br><span class="line">你知道食物链吗？Delia 生物考试的时候，数食物链条数的题目全都错了，因为她总是重复数了几条或漏掉了几条。于是她来就来求助你，然而你也不会啊！写一个程序来帮帮她吧。</span><br><span class="line"></span><br><span class="line">题目描述</span><br><span class="line">给你一个食物网，你要求出这个食物网中最大食物链的数量。</span><br><span class="line"></span><br><span class="line">（这里的“最大食物链”，指的是生物学意义上的食物链，即最左端是不会捕食其他生物的生产者，最右端是不会被其他生物捕食的消费者。）</span><br><span class="line"></span><br><span class="line">Delia 非常急，所以你只有 <span class="number">1</span> 秒的时间。</span><br><span class="line"></span><br><span class="line">由于这个结果可能过大，你只需要输出总数模上 <span class="number">80112002</span> 的结果。</span><br><span class="line"></span><br><span class="line">输入格式</span><br><span class="line">第一行，两个正整数 n、m，表示生物种类 n 和吃与被吃的关系数mm。</span><br><span class="line"></span><br><span class="line">接下来 m 行，每行两个正整数，表示被吃的生物A和吃A的生物B。</span><br><span class="line"></span><br><span class="line">输出格式</span><br><span class="line">一行一个整数，为最大食物链数量模上 <span class="number">80112002</span> 的结果。</span><br></pre></td></tr></table></figure>

<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> mod=<span class="number">80112002</span>;</span><br><span class="line"><span class="type">int</span> n, m, h[<span class="number">5005</span>], ru[<span class="number">5005</span>], chu[<span class="number">5005</span>], f[<span class="number">5005</span>], ans;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">AB</span>&#123;</span><br><span class="line">   <span class="type">int</span> a,b,n;</span><br><span class="line">&#125;d[<span class="number">5000005</span>];</span><br><span class="line">queue&lt;<span class="type">int</span>&gt; q;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">   cin&gt;&gt;n&gt;&gt;m;</span><br><span class="line">   <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=m;i++)&#123;</span><br><span class="line">   	<span class="type">int</span> a, b;</span><br><span class="line">       <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>, &amp;a, &amp;b);</span><br><span class="line">       d[i].a=a, d[i].b=b, d[i].n=h[a], h[a]=i;<span class="comment">//建图</span></span><br><span class="line">       chu[a]++, ru[b]++;</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">       <span class="keyword">if</span>(ru[i]==<span class="number">0</span>) &#123;</span><br><span class="line">           f[i]=<span class="number">1</span>;</span><br><span class="line">           q.<span class="built_in">push</span>(i);</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="keyword">while</span>(!q.<span class="built_in">empty</span>())&#123;</span><br><span class="line">       <span class="type">int</span> a=q.<span class="built_in">front</span>();</span><br><span class="line">       q.<span class="built_in">pop</span>();</span><br><span class="line">       <span class="keyword">for</span>(<span class="type">int</span> k=h[a];k;k=d[k].n)&#123;</span><br><span class="line">           <span class="type">int</span> b=d[k].b;</span><br><span class="line">           f[b]+=f[a];</span><br><span class="line">           f[b]%=mod;</span><br><span class="line">           ru[b]--;</span><br><span class="line">           <span class="keyword">if</span>(ru[b]==<span class="number">0</span>)&#123;</span><br><span class="line">               <span class="keyword">if</span>(chu[b]==<span class="number">0</span>)&#123;</span><br><span class="line">                   ans+=f[b];</span><br><span class="line">                   ans%=mod;</span><br><span class="line">               &#125;<span class="comment">//出度为0的点为食物链终点，记录答案，并且不必入队</span></span><br><span class="line">               <span class="keyword">else</span> q.<span class="built_in">push</span>(b);</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   cout&lt;&lt;ans; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="二分查找"><a href="#二分查找" class="headerlink" title="二分查找"></a>二分查找</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 100005</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> arr[N];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 二分查找函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">binarySearch</span><span class="params">(<span class="type">int</span> arr[], <span class="type">int</span> size, <span class="type">int</span> target, <span class="type">int</span> op)</span> &#123;</span><br><span class="line">    <span class="type">int</span> left = <span class="number">0</span>, right = size - <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">while</span> (left &lt;= right) &#123;</span><br><span class="line">        <span class="type">int</span> mid = left + (right - left) / <span class="number">2</span>;  <span class="comment">// 防止溢出</span></span><br><span class="line">        <span class="keyword">if</span> (arr[mid] == target) &#123;</span><br><span class="line">        	<span class="keyword">if</span>(op==<span class="number">1</span>)&#123;			</span><br><span class="line">        		<span class="keyword">while</span>(arr[mid<span class="number">-1</span>] == target) &#123;</span><br><span class="line">        			mid--;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span> &#123;			</span><br><span class="line">        		<span class="keyword">while</span>(arr[mid+<span class="number">1</span>] == target) &#123;</span><br><span class="line">        			mid++;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">            <span class="keyword">return</span> mid+<span class="number">1</span>; <span class="comment">// 找到目标，返回其索引</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (arr[mid] &lt; target) &#123;</span><br><span class="line">            left = mid + <span class="number">1</span>; <span class="comment">// 目标在右侧</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            right = mid - <span class="number">1</span>; <span class="comment">// 目标在左侧</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">-1</span>; <span class="comment">// 未找到目标</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span> &#123;</span><br><span class="line">	<span class="type">int</span> size, m;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>, &amp;size, &amp;m);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;size;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;arr[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=m;i++) &#123;</span><br><span class="line">		<span class="type">int</span> op, target;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>, &amp;op, &amp;target);</span><br><span class="line">    </span><br><span class="line">    	<span class="type">int</span> result = binarySearch(arr, size, target, op);</span><br><span class="line">    	<span class="keyword">if</span> (result != <span class="number">-1</span>) &#123;</span><br><span class="line">        	<span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, result);</span><br><span class="line">    	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        	<span class="built_in">printf</span>(<span class="string">&quot;Not Found\n&quot;</span>);</span><br><span class="line">   	 	&#125;</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="跨越倍数点"><a href="#跨越倍数点" class="headerlink" title="跨越倍数点"></a>跨越倍数点</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="comment">// 初始化变量 a, b, c, ans, up, down</span></span><br><span class="line">    <span class="type">int</span> a, b, c, ans, up, down;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 读取输入值 a, b, c</span></span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%d%d&quot;</span>, &amp;a, &amp;b, &amp;c);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 特殊情况：如果 a 和 b 在同一个 c 的倍数区间内</span></span><br><span class="line">    <span class="keyword">if</span> (a / c == b / c) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, (b - a + <span class="number">1</span>) / <span class="number">2</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 计算距离 a 和 b 最近的、大于等于 a 和小于等于 b 的 c 的倍数</span></span><br><span class="line">    down = (a + c - <span class="number">1</span>) / c * c;  <span class="comment">// a 的上界（大于等于 a 的最小 c 的倍数）</span></span><br><span class="line">    up = b / c * c;  <span class="comment">// b 的下界（小于等于 b 的最大 c 的倍数）</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 初始化答案 ans</span></span><br><span class="line">    <span class="comment">// 加上从 a 到 down 和从 up 到 b 的操作次数（都用 Y 操作即可）</span></span><br><span class="line">    <span class="comment">// 这两个部分都不包含 c 的倍数，所以可以直接操作</span></span><br><span class="line">    ans = (down - a) / <span class="number">2</span> + (b - up) / <span class="number">2</span> + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// c 的奇偶性处理</span></span><br><span class="line">    <span class="keyword">if</span> (c % <span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="comment">// 如果 c 是偶数，则从 down 到 up 只需用 c/2 次 Y 操作跨过一次</span></span><br><span class="line">        ans += (up - down) / <span class="number">2</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 如果 c 是奇数，则从 down 到 up 每个 c 的倍数都可以用 (c+1)/2 次 Y 操作跨过</span></span><br><span class="line">        ans += (up - down) / c * (c + <span class="number">1</span>) / <span class="number">2</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 输出答案</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="单调队列求滑动窗口最大最小值"><a href="#单调队列求滑动窗口最大最小值" class="headerlink" title="单调队列求滑动窗口最大最小值"></a>单调队列求滑动窗口最大最小值</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ll long long</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 1000005</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> n, k;</span><br><span class="line"><span class="type">int</span> a[N], q[N];  <span class="comment">//q[N]存储数组中元素的下标。</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>, &amp;n, &amp;k);</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;a[i]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> hh = <span class="number">0</span>, tt = <span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)  <span class="comment">//&quot;i&quot;表示数组元素的下标。</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//&quot;hh &lt;= tt&quot;判断队列不为空。</span></span><br><span class="line">        <span class="comment">//判断队头是否已经滑出窗口。当前滑动窗口的大小(i - q[hh] + 1)大于我们设定的滑动窗口的大小(k)，就删除队头。</span></span><br><span class="line">        <span class="keyword">if</span> (hh &lt;= tt &amp;&amp; i - q[hh] + <span class="number">1</span> &gt; k)  <span class="comment">//&quot;q[hh]&quot;表示当前队列的队头。</span></span><br><span class="line">        &#123;</span><br><span class="line">            hh++;</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        <span class="comment">//如果队尾元素大于&quot;待入队元素&quot;，队尾元素就一定不是当前窗口最小值，弹出该队尾元素。</span></span><br><span class="line">        <span class="keyword">while</span> (hh &lt;= tt &amp;&amp; a[q[tt]] &gt;= a[i])</span><br><span class="line">        &#123;</span><br><span class="line">            tt--;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        q[++tt] = i;  <span class="comment">//&quot;待入队元素&quot;进队列。</span></span><br><span class="line">        <span class="keyword">if</span> (i &gt;= k - <span class="number">1</span>)  <span class="comment">//当&quot;滑动窗口&quot;有三个元素时，才开始输出。</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, a[q[hh]]);  <span class="comment">//单调队列具有单调性(递增)，所以队头就是该&quot;滑动窗口&quot;的最小值。</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line"></span><br><span class="line">    hh = <span class="number">0</span>, tt = <span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (hh &lt;= tt &amp;&amp; i - k + <span class="number">1</span> &gt; q[hh])</span><br><span class="line">        &#123;</span><br><span class="line">            hh++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span> (hh &lt;= tt &amp;&amp; a[q[tt]] &lt;= a[i])</span><br><span class="line">        &#123;</span><br><span class="line">            tt--;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        q[++tt] = i;</span><br><span class="line">        <span class="keyword">if</span> (i &gt;= k - <span class="number">1</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, a[q[hh]]);  <span class="comment">//单调队列具有单调性(递减)，所以队头就是该&quot;滑动窗口&quot;的最大值。</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="n皇后问题"><a href="#n皇后问题" class="headerlink" title="n皇后问题"></a>n皇后问题</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> one_way[<span class="number">20</span>];</span><br><span class="line"><span class="type">int</span> all_way[<span class="number">20</span>];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> first = <span class="number">1</span>;</span><br><span class="line"><span class="type">int</span> way = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">is_emp</span><span class="params">(<span class="type">int</span> row, <span class="type">int</span> col)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; row; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span>(all_way[i] == col || <span class="built_in">abs</span>(row - i) == <span class="built_in">abs</span>(col - all_way[i]))</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">place</span><span class="params">(<span class="type">int</span> deter, <span class="type">int</span> to_be)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(deter &gt;= to_be)</span><br><span class="line">    &#123;</span><br><span class="line">        way++;</span><br><span class="line">        <span class="keyword">if</span>(first)</span><br><span class="line">        &#123;</span><br><span class="line">            first = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; to_be; i++)</span><br><span class="line">                one_way[i] = all_way[i];</span><br><span class="line">        &#125;   </span><br><span class="line">    &#125;    </span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; to_be ;i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="built_in">is_emp</span>(deter, i))</span><br><span class="line">            &#123;</span><br><span class="line">                all_way[deter] = i;<span class="comment">//记录数组</span></span><br><span class="line">                <span class="built_in">place</span>(deter + <span class="number">1</span>, to_be);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> n;</span><br><span class="line">    cin &gt;&gt; n;</span><br><span class="line">    <span class="built_in">place</span>(<span class="number">0</span>, n);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(!first)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; way &lt;&lt; endl;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">            cout &lt;&lt; one_way[i] &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span>   cout &lt;&lt; <span class="string">&quot;0&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="贪心看节目"><a href="#贪心看节目" class="headerlink" title="贪心看节目"></a>贪心看节目</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ll long long</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 1000005</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">	<span class="type">int</span> x;</span><br><span class="line">	<span class="type">int</span> y;</span><br><span class="line">&#125; Project;</span><br><span class="line"></span><br><span class="line">Project project[N];</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">cmp</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *a, <span class="type">const</span> <span class="type">void</span>*b)</span>&#123; </span><br><span class="line">    Project p = *(Project*)a; </span><br><span class="line">    Project q = *(Project*)b; </span><br><span class="line">    <span class="keyword">return</span> p.y - q.y; </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span> &#123;</span><br><span class="line">	<span class="type">int</span> n;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;project[i].x,&amp;project[i].y);</span><br><span class="line">	&#125;</span><br><span class="line">	qsort(project+<span class="number">1</span>, n, <span class="keyword">sizeof</span>(Project), cmp);</span><br><span class="line">	<span class="type">int</span> cnt=<span class="number">0</span>, time=<span class="number">0</span>;</span><br><span class="line">	time=project[<span class="number">1</span>].y;</span><br><span class="line">	cnt++;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">2</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(time&lt;=project[i].x) &#123;</span><br><span class="line">			time = project[i].y;</span><br><span class="line">			cnt++;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, cnt);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="高精度加减法"><a href="#高精度加减法" class="headerlink" title="高精度加减法"></a>高精度加减法</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ll long long</span></span><br><span class="line"> </span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 5005</span></span><br><span class="line"></span><br><span class="line"><span class="type">char</span> a1[N], b1[N]; <span class="comment">//用字符存储数字</span></span><br><span class="line"><span class="type">int</span> a[N], b[N], c[N]; <span class="comment">//c[i] 用来储存每位相加的结果</span></span><br><span class="line"><span class="type">int</span> isNegativeA, isNegativeB;</span><br><span class="line"><span class="type">void</span> <span class="title function_">add</span><span class="params">()</span> &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="type">int</span> len_a, len_b, len_c = <span class="number">1</span>, x, i;</span><br><span class="line"> 	<span class="comment">// 去除前导0</span></span><br><span class="line">    <span class="keyword">while</span> (a1[<span class="number">0</span>] == <span class="string">&#x27;0&#x27;</span> &amp;&amp; <span class="built_in">strlen</span>(a1) &gt; <span class="number">1</span>) memmove(a1, a1 + <span class="number">1</span>, <span class="built_in">strlen</span>(a1));</span><br><span class="line">    <span class="keyword">while</span> (b1[<span class="number">0</span>] == <span class="string">&#x27;0&#x27;</span> &amp;&amp; <span class="built_in">strlen</span>(b1) &gt; <span class="number">1</span>) memmove(b1, b1 + <span class="number">1</span>, <span class="built_in">strlen</span>(b1));</span><br><span class="line">    len_a = <span class="built_in">strlen</span>(a1);</span><br><span class="line">    len_b = <span class="built_in">strlen</span>(b1);</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;len_a; i++) a[len_a - i] = a1[i] - <span class="string">&#x27;0&#x27;</span>; <span class="comment">// 将加数放进a数组</span></span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;len_b; i++) b[len_b - i] = b1[i] - <span class="string">&#x27;0&#x27;</span>; <span class="comment">// 将另一个加数放进b数组</span></span><br><span class="line"> </span><br><span class="line">    x = <span class="number">0</span>; <span class="comment">// x为进位</span></span><br><span class="line">    <span class="keyword">while</span>(len_c &lt;= len_a || len_c &lt;= len_b) &#123;</span><br><span class="line">        c[len_c] = a[len_c] + b[len_c] + x; <span class="comment">// 两数相加，再加上前两个数进位的</span></span><br><span class="line">        x = c[len_c] / <span class="number">10</span>; <span class="comment">// 刷新进位</span></span><br><span class="line">        c[len_c] %= <span class="number">10</span>; <span class="comment">// 进位后剩下的</span></span><br><span class="line">        len_c++; <span class="comment">//位数加1</span></span><br><span class="line">    &#125;</span><br><span class="line">    c[len_c] = x;</span><br><span class="line">    <span class="keyword">if</span>(c[len_c] == <span class="number">0</span>) &#123; <span class="comment">//判断首位是否为0</span></span><br><span class="line">        len_c--; <span class="comment">// 不输出此位</span></span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=len_c; i&gt;=<span class="number">1</span>; i--) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>, c[i]); <span class="comment">//输出每一位的数</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">minus</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">int</span> lena, lenb, lenc, i;</span><br><span class="line"> </span><br><span class="line"> 	<span class="comment">// 去除前导0</span></span><br><span class="line">    <span class="keyword">while</span> (a1[<span class="number">0</span>] == <span class="string">&#x27;0&#x27;</span> &amp;&amp; <span class="built_in">strlen</span>(a1) &gt; <span class="number">1</span>) memmove(a1, a1 + <span class="number">1</span>, <span class="built_in">strlen</span>(a1));</span><br><span class="line">    <span class="keyword">while</span> (b1[<span class="number">0</span>] == <span class="string">&#x27;0&#x27;</span> &amp;&amp; <span class="built_in">strlen</span>(b1) &gt; <span class="number">1</span>) memmove(b1, b1 + <span class="number">1</span>, <span class="built_in">strlen</span>(b1));</span><br><span class="line">    lena = <span class="built_in">strlen</span>(a1);</span><br><span class="line">    lenb = <span class="built_in">strlen</span>(b1);</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;lena; i++) a[lena - i] = (<span class="type">int</span>)a1[i] - <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;lenb; i++) b[lenb - i] = (<span class="type">int</span>)b1[i] - <span class="string">&#x27;0&#x27;</span>; <span class="comment">//逆序存放排列</span></span><br><span class="line"> </span><br><span class="line">    i = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">while</span>(i &lt;= lena || i &lt;= lenb) &#123;</span><br><span class="line">        <span class="keyword">if</span>(a[i] &lt; b[i]) &#123;</span><br><span class="line">            c[i] = a[i] + <span class="number">10</span> - b[i];</span><br><span class="line">            a[i+<span class="number">1</span>]--; <span class="comment">//借位处理</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> &#123;</span><br><span class="line">            c[i] = a[i] - b[i]; </span><br><span class="line">        &#125;</span><br><span class="line">        i++;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    lenc = i;</span><br><span class="line">    <span class="keyword">while</span>(c[lenc] == <span class="number">0</span> &amp;&amp; lenc &gt; <span class="number">1</span>) &#123; <span class="comment">//如果最后一位是0，是需要输出的</span></span><br><span class="line">        lenc--;   <span class="comment">// 不输出首位0</span></span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=lenc; i&gt;=<span class="number">1</span>; i--) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>, c[i]); <span class="comment">//输出每一位的数</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">reset</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="built_in">memset</span>(a, <span class="number">0</span>, <span class="keyword">sizeof</span>(a));</span><br><span class="line">    <span class="built_in">memset</span>(b, <span class="number">0</span>, <span class="keyword">sizeof</span>(b));</span><br><span class="line">    <span class="built_in">memset</span>(c, <span class="number">0</span>, <span class="keyword">sizeof</span>(c));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span> &#123;</span><br><span class="line"> 	reset();</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%s%s&quot;</span>, a1, b1); <span class="comment">//输入两个加数</span></span><br><span class="line">    <span class="comment">// 检查是否为负数，并移除符号</span></span><br><span class="line">    <span class="keyword">if</span> (a1[<span class="number">0</span>] == <span class="string">&#x27;-&#x27;</span>) &#123;</span><br><span class="line">        isNegativeA = <span class="number">1</span>;</span><br><span class="line">        memmove(a1, a1 + <span class="number">1</span>, <span class="built_in">strlen</span>(a1));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (b1[<span class="number">0</span>] == <span class="string">&#x27;-&#x27;</span>) &#123;</span><br><span class="line">        isNegativeB = <span class="number">1</span>;</span><br><span class="line">        memmove(b1, b1 + <span class="number">1</span>, <span class="built_in">strlen</span>(b1));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (isNegativeA &amp;&amp; isNegativeB) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;-&quot;</span>);  <span class="comment">// 如果两者都是负数，则结果也是负数</span></span><br><span class="line">        add();</span><br><span class="line">    &#125;  <span class="keyword">else</span> <span class="keyword">if</span>(!isNegativeA &amp;&amp; !isNegativeB)&#123;</span><br><span class="line">        add();</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 处理减法，这里省略了代码。可以根据需要添加</span></span><br><span class="line">        <span class="keyword">if</span>(isNegativeB == <span class="number">1</span>) &#123;</span><br><span class="line">        	minus();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> &#123;</span><br><span class="line">        	<span class="built_in">printf</span>(<span class="string">&quot;-&quot;</span>);</span><br><span class="line">        	minus();</span><br><span class="line">		&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="矩阵的行列式"><a href="#矩阵的行列式" class="headerlink" title="矩阵的行列式"></a>矩阵的行列式</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAX 20</span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">swap</span><span class="params">(<span class="type">double</span>* a,<span class="type">double</span>*b)</span>&#123;</span><br><span class="line">	<span class="type">double</span> tmp = *a;</span><br><span class="line">	*a = *b;</span><br><span class="line">	*b = tmp;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">swaparr</span><span class="params">(<span class="type">double</span> arr[MAX][MAX],<span class="type">int</span> a,<span class="type">int</span> b,<span class="type">int</span> n)</span>&#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;n;i++)</span><br><span class="line">		swap(&amp;arr[i][a],&amp;arr[i][b]);	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="type">int</span> i,j,k;</span><br><span class="line">	<span class="type">int</span> n=<span class="number">3</span>;				<span class="comment">//阶数 </span></span><br><span class="line">	<span class="type">int</span> sign=<span class="number">0</span>;			<span class="comment">//行列式交换一次需要改变符号，此变量记录交换次数 </span></span><br><span class="line">	<span class="type">double</span> tmp;			<span class="comment">//暂存乘积因子 </span></span><br><span class="line">	<span class="type">double</span> arr[MAX][MAX];</span><br><span class="line">	<span class="type">double</span> sum=<span class="number">1.0</span>;     <span class="comment">//结果 </span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">// scanf(&quot;%d&quot;,&amp;n);</span></span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;n;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(j=<span class="number">0</span>;j&lt;n;j++)</span><br><span class="line">			<span class="built_in">scanf</span>(<span class="string">&quot;%lf&quot;</span>,&amp;arr[i][j]);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;n<span class="number">-1</span>;i++)&#123;</span><br><span class="line">		k=<span class="number">1</span>;</span><br><span class="line">		<span class="keyword">while</span>(arr[i][i]==<span class="number">0.0</span>&amp;&amp;i+k&lt;n)&#123;	<span class="comment">//确保标准数不为0 </span></span><br><span class="line">			swaparr(arr,i,i+(k++),n);</span><br><span class="line">			sign++;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(arr[i][i]==<span class="number">0.0</span>&amp;&amp;i+k==n)&#123;		<span class="comment">//整个一行都为0 </span></span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;0&quot;</span>);</span><br><span class="line">			<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">for</span>(j=i+<span class="number">1</span>;j&lt;n;j++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(arr[j][i]==<span class="number">0.0</span>)			<span class="comment">//如为0则那一行不用化简 </span></span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			<span class="keyword">else</span>&#123;</span><br><span class="line">				tmp = -(<span class="type">double</span>)arr[j][i]/arr[i][i];		<span class="comment">//保存乘积因子 </span></span><br><span class="line">				<span class="keyword">for</span>(k=i;k&lt;n;k++)</span><br><span class="line">					arr[j][k] += (tmp*arr[i][k]);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;n;i++)</span><br><span class="line">		sum *= arr[i][i];</span><br><span class="line">	<span class="comment">//if(sign%2==0)				//交换偶数次符号仍为正 </span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%.1f&quot;</span>,sum);</span><br><span class="line">	<span class="comment">//else</span></span><br><span class="line">		<span class="comment">//printf(&quot;-%.1f&quot;,sum);	</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer"><script src="//sdk.jinrishici.com/v2/browser/jinrishici.js"></script>
<script>
  jinrishici.load((result) => {
    let jrsc = document.getElementById('jrsc');
    const data = result.data;
    let author = data.origin.author;
    let title = '《' + data.origin.title + '》';
    let content = data.content.substr(0, data.content.length - 1);
    let dynasty = data.origin.dynasty.substr(0, data.origin.dynasty.length - 1);
    jrsc.innerText = content + ' @ ' + dynasty + '·' + author + title;
  });
</script>
<div style="text-align: center"><span id="jrsc" >正在加载今日诗词....</span></div>

          <div class="post-tags">
              <a href="/tags/%E7%AE%97%E6%B3%95/" rel="tag"># 算法</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/07/15/2022-07-15-%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="prev" title="深度学习笔记">
                  <i class="fa fa-chevron-left"></i> 深度学习笔记
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/08/29/%E4%BF%9D%E7%A0%94%E9%9D%A2%E8%AF%95/" rel="next" title="保研面试">
                  保研面试 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="waline"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Mathieu Lin</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div><script color="0,0,255" opacity="0.5" zIndex="-1" count="99" src="https://cdnjs.sourcegcdn.com//ajax/libs/canvas-nest.js/1.0.1/canvas-nest.js"></script>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.5.0/pjax.min.js" integrity="sha256-3NkoLDrmHLTYj7csHIZSr0MHAFTXth7Ua/DDt4MRUAg=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  




<script class="next-config" data-name="waline" type="application/json">{"lang":"zh-CN","enable":true,"serverURL":"https://blog-commeny-mh5e4d5jo-mathieulin.vercel.app","cssUrl":"https://unpkg.com/@waline/client@v2/dist/waline.css","commentCount":true,"pageview":false,"el":"#waline","comment":true,"libUrl":"//unpkg.com/@waline/client@v2/dist/waline.js","path":"/2023/08/29/%E6%9D%BF%E5%AD%90/"}</script>
<link rel="stylesheet" href="https://unpkg.com/@waline/client@v2/dist/waline.css">
<script>
document.addEventListener('page:loaded', () => {
  NexT.utils.loadComments(CONFIG.waline.el).then(() =>
    NexT.utils.getScript(CONFIG.waline.libUrl, { condition: window.Waline })
  ).then(() => 
    Waline.init(Object.assign({}, CONFIG.waline,{ el: document.querySelector(CONFIG.waline.el) }))
  );
});
</script>

</body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>
